给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针(next)。
public class Solution {
public TreeLinkNode GetNext(TreeLinkNode pNode) {
if(pNode.right!=null){
TreeLinkNode p=pNode.right;
while(p.left!=null){
p=p.left;
}
return p;
}else{
if(pNode.next==null) return null;
//pNode为父节点的左节点
if(pNode.next.left==pNode) return pNode.next;
else{//pNode为父节点的右节点
if(pNode.next.next==null) return null;
if(pNode.next.next.left==pNode.next) return pNode.next.next;
else return null;
}
}
}
}