Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
TreeNode ret = null;
while(root != null){
if(root.val > p.val){
ret = root;
root = root.left;
}else{
root = root.right;
}
}
return ret;
}
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if(root == null)
return null;
if(root.val > p.val){
TreeNode left = inorderSuccessor(root.left, p);
return left != null ? left : root;
}else{
return inorderSuccessor(root.right, p);
}
}
public TreeNode predecessor(TreeNode root, TreeNode p) {
TreeNode ret = null;
while(root != null){
if(root.val < p.val){
ret = root;
root = root.right;
}else{
root = root.left;
}
}
return ret;
}
public TreeNode predecessor(TreeNode root, TreeNode p) {
if(root == null)
return null;
if(root.val < p.val){
TreeNode right = predecessor(root.right, p);
return right != null : right : root;
}elsel{
return predecessor(root.left, p);
}
}