-
Notifications
You must be signed in to change notification settings - Fork 0
/
InorderSuccessor.java
49 lines (44 loc) · 1.54 KB
/
InorderSuccessor.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
47
48
49
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class InorderSuccessor {
/*
Runtime: 3 ms, faster than 60.50% of Java online submissions for Inorder Successor in BST.
Memory Usage: 49.3 MB, less than 36.76% of Java online submissions for Inorder Successor in BST.
*/
TreeNode node=null; //for storing the final result
boolean isNodefound=false; // to check while traversing we found the node or not
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
inOrder(root,p);
return node;
}
public void inOrder(TreeNode root,TreeNode p)
{
if(root==null)
{
return;
}
if(root==p)
{
isNodefound=true; // we set the boolean variable that the node whose Inorder successor is to be found is found while traversing the tree
}
inOrder(root.left,p);
//Going ahead when the node is found and if the node is not yet assigned value then set the node value if the value of the current node is greater than the p node
if(isNodefound && node==null)
{
if(root.val>p.val)
{
node=root;
return;
}
}
if(node!=null) return; // this line can be skipped but its just to make sure when the node value is assigned then it should compute any further
inOrder(root.right,p);
}
}