Skip to content

Commit 78018ce

Browse files
authored
Merge pull request neetcode-gh#1871 from tahsintunan/669
Create 669. Trim a Binary Search Tree
2 parents 2056c01 + 3c73d15 commit 78018ce

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

go/0669-trim-a-binary-search-tree.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func trimBST(root *TreeNode, low int, high int) *TreeNode {
10+
if root == nil {
11+
return nil
12+
}
13+
if root.Val > high {
14+
return trimBST(root.Left, low, high)
15+
}
16+
if root.Val < low {
17+
return trimBST(root.Right, low, high)
18+
}
19+
root.Left = trimBST(root.Left, low, high)
20+
root.Right = trimBST(root.Right, low, high)
21+
22+
return root
23+
}

0 commit comments

Comments
 (0)