标签:style blog color os ar div amp log
5
/ 2 6
/ \ 1 4 7
/
3
class Node{
Node left;
Node right;
Node parent;
int val;
}
/**
1.如果有右子树,则结果为右子树的最左节点。
2.如果没有右子树,则需要回到父节点,如果当前节点是父节点的左子树,则父节点就是结果,如果不是继续向上再找父节点。
*/
public TreeNode inorderSucc(TreeNode n){
if(n==null)
return null;
if(n.right!=null)
return leftMost(n.right);
TreeNode child = n;
TreeNode par =child.parent;
while(par!=null&&par.right==child){
child=par;
par=par.n;
}
return par;
}
private TreeNode leftMost(TreeNode n){
if(n==null)
return null;
while(n.left!=null)
n=n.left;
return n;
}
4.6 找出二叉树中指定节点的下一个节点(中序后继),假定每个节点有父指针。,布布扣,bubuko.com
4.6 找出二叉树中指定节点的下一个节点(中序后继),假定每个节点有父指针。
标签:style blog color os ar div amp log
原文地址:http://www.cnblogs.com/jdflyfly/p/3926018.html