-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathInorderSucc.java
More file actions
42 lines (34 loc) · 735 Bytes
/
InorderSucc.java
File metadata and controls
42 lines (34 loc) · 735 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
33
34
35
36
37
38
39
40
41
42
package Trees;
/**
* Author - archit.s
* Date - 04/11/18
* Time - 5:12 PM
*/
public class InorderSucc {
public TreeNode getSuccessor(TreeNode a, int b) {
TreeNode succ = null;
TreeNode cur = a;
while(cur!=null && cur.val!=b){
if(cur.val > b){
succ = cur;
cur = cur.left;
}
else{
cur = cur.right;
}
}
if(cur == null){
return null;
}
if(cur.right != null){
TreeNode t = cur.right;
while(t.left!=null){
t = t.left;
}
return t;
}
else{
return succ;
}
}
}