-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrimBST.java
More file actions
32 lines (25 loc) · 982 Bytes
/
TrimBST.java
File metadata and controls
32 lines (25 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package BinarySearchTrees;
public class TrimBST {
public static void helper(Node root, int low, int high) {
if (root == null) return;
while (root.left != null) {
if (root.left.val < low) root.left = root.left.right;
else if (root.left.val > high) root.left = root.left.left;
else break;
}
while (root.right != null) {
if (root.right.val < low) root.right = root.right.right;
else if (root.right.val > high) root.right = root.right.left;
else break;
}
helper(root.left, low, high);
helper(root.right, low, high);
}
public static void main(String[] args) {
String[] arr = {"50", "20", "60", "17", "34", "55", "89", "10", "", "28", "", "", "", "70", "", "", "14"};
Node root = Construct.construct(arr);
Node parent = new Node(Integer.MAX_VALUE);
parent.left = root;
helper(parent, 20, 50);
}
}