-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInorderSuccessorBST.java
More file actions
37 lines (33 loc) · 1.01 KB
/
InorderSuccessorBST.java
File metadata and controls
37 lines (33 loc) · 1.01 KB
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
/*
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
Note: If the given node has no in-order successor in the tree, return null.
*/
public class InorderSuccessorBST {
//O(h) time and O(1) space
//Assuming BST has no duplicates!
public static TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if (p == null) {
return null;
}
int key = p.val;
TreeNode successor = null;
for(TreeNode cur = root; cur != null;) {
if (key < cur.val) {
successor = cur;
cur = cur.left;
} else {
cur = cur.right;
}
}
return successor;
}
public static void main (String[] args) {
Integer[] t1 = new Integer[]{9, 4, 10, 2, 6, null, 13, null, null, 5, null, 11, null};
TreeNode root = TreeNode.createFromArray(t1);
TreeNode.printLevels(root);
TreeNode curNode = root.right.right;
TreeNode successor = inorderSuccessor(root, curNode);
System.out.println("successor of " + curNode.val + " is "
+ ((successor == null) ? null : successor.val));
}
}