Skip to content

Commit 5e51b1c

Browse files
committed
added TrimBST
1 parent 1abd2ad commit 5e51b1c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Trees/TrimBinarySearchTree.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Trees;
2+
3+
public class TrimBinarySearchTree {
4+
static class TreeNode {
5+
int val;
6+
TreeNode left,right;
7+
}
8+
9+
private TreeNode trimBST(TreeNode root, int low,int high) {
10+
if(root == null) return null;
11+
12+
if(root.val >= low && root.val <= high) {
13+
root.left = trimBST(root.left,low,high);
14+
root.right = trimBST(root.right,low,high);
15+
}
16+
else if(root.val < low) {
17+
root = trimBST(root.right,low,high);
18+
}
19+
else if(root.val > high) {
20+
root = trimBST(root.left,low,high);
21+
}
22+
23+
return root;
24+
}
25+
}

0 commit comments

Comments
 (0)