/*
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)
{
TreeLinkNode current = null;
if (pNode.right != null){
current = pNode.right;
while (current.left != null){current = current.left;}
return current;
}
current = pNode.next;
if (current == null) return null;
if (current.left == pNode){
return current;
}
while (current.next != null){
if (current == current.next.left){
return current.next;
}
current = current.next;
}
return null;
}
}