Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
The successor of a node p is the node with the smallest key greater than p.val.
Note:
- If the given node has no in-order successor in the tree, return null.
- It's guaranteed that the values of the tree are unique.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
List<TreeNode> inorder = new ArrayList<>();
helper(root,inorder);
for(TreeNode node : inorder){
if(node.val > p.val)
return node;
}
return null;
}
public void helper(TreeNode root, List<TreeNode> list){
if(root != null){
helper(root.left, list);
list.add(root);
helper(root.right, list);
}
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
// case 1 : when p have a right node, we start from right of p
if(p.right != null){
return findMinimum(p.right);
}
// case 2 : when p doesn't have right node, we start from root
TreeNode inorderSuccessor = null;
while(root != null){
if(root.val > p.val){
inorderSuccessor = root;
root = root.left;
} else if(root.val < p.val){
root = root.right;
} else {
break;
}
}
return inorderSuccessor;
}
private TreeNode findMinimum(TreeNode root){
while(root.left != null)
root = root.left;
return root;
}
}