Created
March 14, 2013 21:54
-
-
Save Ray1988/5165605 to your computer and use it in GitHub Desktop.
find the next node of given node in binary search tree.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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