Skip to content

Commit dbcab98

Browse files
committed
Create 0669-trim-a-binary-search-tree.kt
1 parent 90ea69a commit dbcab98

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: kotlin/0669-trim-a-binary-search-tree.kt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Example:
3+
* var ti = TreeNode(5)
4+
* var v = ti.`val`
5+
* Definition for a binary tree node.
6+
* class TreeNode(var `val`: Int) {
7+
* var left: TreeNode? = null
8+
* var right: TreeNode? = null
9+
* }
10+
*/
11+
class Solution {
12+
fun trimBST(root: TreeNode?, low: Int, high: Int): TreeNode? {
13+
if(root == null)
14+
return null
15+
if(root.`val` < low)
16+
return trimBST(root.right, low, high)
17+
else if(root.`val` > high)
18+
return trimBST(root.left, low, high)
19+
root.left = trimBST(root.left, low, high)
20+
root.right = trimBST(root.right, low, high)
21+
return root
22+
}
23+
}

0 commit comments

Comments
 (0)