/*
public class TreeLinkNode {
int val;
TreeLinkNode left = null;
TreeLinkNode right = null;
TreeLinkNode next = null;
TreeLinkNode(int val) {
this.val = val;
}
}
*/
import java.util.*;
public class Solution {
private TreeLinkNode ans = null;
private boolean isFound = false;
public TreeLinkNode GetNext(TreeLinkNode pNode) {
TreeLinkNode root = pNode;
while(root.next != null) {
root = root.next;
}
inOrderTrav(root, pNode);
return ans;
}
private void inOrderTrav(TreeLinkNode root, TreeLinkNode target) {
if(root == null) {
return;
}
inOrderTrav(root.left, target);
if(isFound && ans == null) {
ans = root;
}
if(root == target) {
isFound = true;
}
inOrderTrav(root.right, target);
}
}