[剑指offer]二叉树的下一个节点

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

}

猜你喜欢

转载自blog.csdn.net/Shannon076/article/details/81186108