(剑指offer)二叉树的下一个节点by java
二叉树的下一个结点
http://www.nowcoder.com/questionTerminal/9023a0c988684a53960365b889ceaf5e
两种情况:
1.有柚子树
2.没有柚子树
没有的时候就是看爸爸
public class Solution {
public TreeLinkNode GetNext(TreeLinkNode pNode)
{
//两种情况,有柚子树和无柚子树
if(pNode.right!=null){
pNode=pNode.right;
while(pNode.left!=null){
pNode=pNode.left;
}
return pNode;
}
TreeLinkNode baba=pNode.next;
while(baba!=null){
if(baba.left==pNode){
return baba;
}
pNode=baba;
baba=baba.next;
}
return null;
}
}