-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBSTSearcher.java
46 lines (36 loc) · 1.48 KB
/
BSTSearcher.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package binary_search_trie;
public class BSTSearcher {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public TreeNode searchBST(TreeNode root, int val) {
if (root == null || root.val == val) return root;
return val < root.val ? searchBST(root.left, val) : searchBST(root.right, val);
}
private static TreeNode constructTree(Integer[] arr, int i) {
if (i >= arr.length || arr[i] == null) return null;
TreeNode root = new TreeNode(arr[i]);
root.left = constructTree(arr, 2 * i + 1);
root.right = constructTree(arr, 2 * i + 2);
return root;
}
private static boolean isEqual(TreeNode a, TreeNode b) {
if (a == null && b == null) return true;
if (a == null || b == null) return false;
return a.val == b.val && isEqual(a.left, b.left) && isEqual(a.right, b.right);
}
public static void main(String[] args) {
BSTSearcher searcher = new BSTSearcher();
TreeNode root1 = constructTree(new Integer[]{4,2,7,1,3}, 0);
TreeNode expected1 = constructTree(new Integer[]{2,1,3}, 0);
assert isEqual(searcher.searchBST(root1, 2), expected1) : "Test case 1 failed";
TreeNode root2 = constructTree(new Integer[]{4,2,7,1,3}, 0);
assert searcher.searchBST(root2, 5) == null : "Test case 2 failed";
System.out.println("All test cases passed!");
}
}