Skip to content

Instantly share code, notes, and snippets.

@Ray1988
Created March 14, 2013 21:54
Show Gist options
  • Save Ray1988/5165605 to your computer and use it in GitHub Desktop.
Save Ray1988/5165605 to your computer and use it in GitHub Desktop.
find the next node of given node in binary search tree.
TreeNode findNextNode(TreeNode r){
if (r==null) rerturn null;
if(r.father==null||r.right!=null){
return leftMost(r.right);
}
else{
TreeNode x=n;
TreeNode f=n.father;
while(f!=null&&f.left!=x){
x=f;
f=f.father;
}
return f;
}
}
public TreeNode leftMost(TreeNode r){
if(r==null)return null;
while(r.left!=null){
r=r.left
}
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment