Sword finger offer -- the next node in a binary tree

Given a binary tree and one of its nodes, find the next node in the middle order traversal order and return. Note that the nodes in the tree contain not only left and right child nodes, but also pointers to the parent nodes.

public class TreeLinkNode {
    int val;
    TreeLinkNode left = null;
    TreeLinkNode right = null;
    TreeLinkNode next = null;

    TreeLinkNode(int val) {
        this.val = val;
    }
}

My code

public class Solution {
    public TreeLinkNode GetNext(TreeLinkNode pNode)
    {
        if(pNode == null){
            return null;
        }
        if(pNode.right != null){
            return getNextNode(pNode.right);
        }
        
        if(pNode.next == null){
            return null;
        }
        if(pNode.next.left == pNode){
            return pNode.next;
        }
        while(pNode.next.right == pNode){
            pNode = pNode.next;
            if(pNode.next == null){
                return null;
            }
        }
        return pNode.next;
    }
    public TreeLinkNode getNextNode(TreeLinkNode pNode){
        if(pNode == null){
            return null;
        }
        TreeLinkNode result = null;
        result = getNextNode(pNode.left);
        if(result == null)
            result = pNode;
        return result;
    }
}
  • Middle order traversal is to directly traverse the left subtree of the node, then the node itself, and then the right subtree of the node.
  • Preorder traversal is first the node itself, then the left subtree, and then the right subtree.
  • Post order traversal is first left subtree, then right subtree, and finally node itself
  • The traversal rule in the subtree is the same as above

He said to find the next node under the middle order traversal. This node can be divided into two situations

  1. This node has right subtree
  2. This node has no right subtree
  • The first kind of comparison is relatively simple to handle. You can directly traverse the right node in the middle order, and return the rightmost node traversed.
  • The second situation is divided into two situations
    1. This node is the left child of the parent node
    2. This node is the right child of the parent node
    • Here, the node is the left child of the parent node, which is relatively simple. Just return the parent node directly
    • If it is the right child node of the parent node, it needs to move up until the corresponding node is not the right node of the parent node (that is, the left node). Since it is the left node of the parent node, return the parent node of this node, or traverse to the root node and return null;

So we need to introduce a function that must traverse in progress and will traverse the first node, namely getNextNode above

Because this code is mainly based on the idea of decomposition, first with a simple recursive implementation.
If you don't nest other functions and use while to solve them, the code will be much simpler
as follows

public class Solution {
    public TreeLinkNode GetNext(TreeLinkNode pNode)
    {
        if(pNode == null){
            return null;
        }
        if(pNode.right != null){
            pNode = pNode.right;
            while(pNode.left != null){
                pNode = pNode.left;
            }
            return pNode;
        }
        while(pNode.next != null){
            if(pNode.next.left== pNode){
                return pNode.next;
            }
            pNode = pNode.next;
        }
        return null;
    }
}

Posted by regexpert on Wed, 29 Apr 2020 22:38:14 -0700