-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInorderSuccessor.java
More file actions
31 lines (30 loc) · 902 Bytes
/
Copy pathInorderSuccessor.java
File metadata and controls
31 lines (30 loc) · 902 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
package leetcode;
public class InorderSuccessor {
TreeNode prev = null;
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if(root == null) return null;
TreeNode left = null;
TreeNode right = null;
if(p.val < root.val){
left = inorderSuccessor(root.left, p);
}
if(left != null) return left;
if(prev == p){
return root;
}
prev = root;
if(p.val >= root.val){
right = inorderSuccessor(root.right, p);
}
return right;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(3);
root.left = new TreeNode(2);
root.left.left = new TreeNode(1);
root.right = new TreeNode(5);
root.right.left = new TreeNode(4);
InorderSuccessor is = new InorderSuccessor();
System.out.println(is.inorderSuccessor(root, root.right.left).val);
}
}