题解 | #二叉树的下一个结点#
二叉树的下一个结点
https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e
public class Solution {
public TreeLinkNode GetNext(TreeLinkNode pNode) {
return pNode.right == null ? toUp(pNode) : toRight(pNode.right);
}
TreeLinkNode toRight(TreeLinkNode node) {
if (node.left == null) return node;
return toRight(node.left);
}
TreeLinkNode toUp(TreeLinkNode node) {
if (node.next == null) return null;
if (node == node.next.left) return node.next;
return toUp(node.next);
}
}


