/*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;
}
//该结点没有右子树的话
//向上找, 向上找看看能不能找到一个结点为父节点的左子结点
//该结点的父节点p为p的父节点的左子节点,返回父节点的父节点
while(pNode.next != null && pNode.next.left != pNode){
pNode = pNode.next;
}
//找不着说明遍历完了,为空
//找到就返回
return pNode.next;
}
}