/*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;
}
}