-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchBST.java
41 lines (37 loc) · 1.01 KB
/
SearchBST.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
/**
* Search in a Binary Search Tree (BST)
*
* Given the root node of a BST and a value. You need to find the node in the BST that the node's value equals the
* given value. Return the subtree rooted with that node. If such node doesn't exist, return NULL.
*
* Note: An empty tree is represented by NULL, therefore you would see the expected output (serialized tree format) as [], not null.
*
* e.g. IN: [4,2,7,1,3] and 2, OUT: [2,1,3]
* Given the tree:
* 4
* / \
* 2 7
* / \
* 1 3
*
* Return the subtree:
* 2
* / \
* 1 3
*
* Complexity: O(log n) avg time, O(n) worst time, O(1) space
*/
public class SearchBST {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode (int x) { val = x; }
}
public TreeNode searchBST(TreeNode root, int val) {
while (root != null && val != root.val) {
root = val < root.val ? root.left : root.right;
}
return root;
}
}