/* public class TreeLinkNode { int val; TreeLinkNode left = null; TreeLinkNode right = null; TreeLinkNode next = null; TreeLinkNode(int val) { this.val = val; } } */ publicclassSolution{ public TreeLinkNode GetNext(TreeLinkNode pNode) { if(pNode == null){ returnnull; } //find right child's leftest child (if has right child) if(pNode.right != null){ pNode = pNode.right; while(pNode.left != null){ pNode = pNode.left; } return pNode; } //if node is not root while(pNode.next != null){ //find node it is parent's left child if(pNode == pNode.next.left){ //return this node's parent return pNode.next; } pNode = pNode.next; } //node is root and it has not right child returnnull; } }