主要要考虑到:当该节点没有右子树时,然后要判断其为父节点的左子树还是右子树,同时特别要注意!!!边界条件,当该节点没有父节点和右子树时的判断(当然也要判断该节点是否为null)。代码如下
/*
public class TreeLinkNode {
int val;
TreeLinkNode left = null;
TreeLinkNode right = null;
TreeLinkNode next = null;
TreeLinkNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public TreeLinkNode GetNext(TreeLinkNode pNode)
{
if(pNode==null)
return null;
if(pNode.right!=null){
pNode=pNode.right;
while(pNode.left!=null) pNode=pNode.left;
return pNode;
}
else if(pNode.next==null)
return null;
else if(pNode.next.left==pNode)
return pNode.next;
else{
while(pNode.next!=null&&pNode.next.left!=pNode){
pNode=pNode.next;
}
return pNode.next;
}
}
}