Skip to content

Commit

Permalink
Create 0669-trim-a-binary-search-tree.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a committed Jan 20, 2023
1 parent 90ea69a commit dbcab98
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions kotlin/0669-trim-a-binary-search-tree.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun trimBST(root: TreeNode?, low: Int, high: Int): TreeNode? {
if(root == null)
return null
if(root.`val` < low)
return trimBST(root.right, low, high)
else if(root.`val` > high)
return trimBST(root.left, low, high)
root.left = trimBST(root.left, low, high)
root.right = trimBST(root.right, low, high)
return root
}
}

0 comments on commit dbcab98

Please sign in to comment.