The sword refers to Offer-Question 8 (Java Edition): In inorder traversal, find the next node of the binary tree

Reference from: "Sword Pointing Offer - Famous Enterprise Interviewer Talking About Typical Programming Questions"

Question: In inorder traversal, find the next
node of a binary tree Given a binary tree and one of its nodes, how to find the next node in inorder traversal? In addition to two pointers to the left and right child nodes, the nodes in the tree also have a pointer to the parent node.

Main idea:
There are three cases:
1. The right subtree of the node is not empty : its next node is the leftmost node of the right subtree.
2. The node's right subtree is empty , but it is the left node of its parent : the next node is its parent.
3. The right subtree of this node is empty , but it is the right node of its parent node : traverse up along the pointer to the parent node until a node A is found, A is the left node of its parent node (A== A.parent.left), then A.parent (A's parent node) is the next node required.
If none of the three conditions are satisfied, the next node is empty.

Key point: The characteristics of in-order traversal: first the left node, then the parent node, and finally the right node

Time complexity: O (tree height)

public class NextTreeNode
{
    public static void main(String[] args)
    {
//          10
//         /
//        6
//       /\
//      4  8
        TreeLinkNode node8 = getTree();
        TreeLinkNode next = getNextNodeFromInorder(node8);
        System.out.println(next.val); //10
    }

    private static TreeLinkNode getTree()
    {
        TreeLinkNode root = new TreeLinkNode(10);
        TreeLinkNode node6 = new TreeLinkNode(6);
        TreeLinkNode node4 = new TreeLinkNode(4);
        TreeLinkNode node8 = new TreeLinkNode(8);
        root.left = node6;
        node6.parent = root;
        node6.left = node4;
        node4.parent = node6;
        node6.right = node8;
        node8.parent = node6;
        return node8;
    }

    /**
     * 二叉树的下一个结点(中序遍历)
     * @param pNode
     * @return
     */
    public static TreeLinkNode getNextNodeFromInorder(TreeLinkNode pNode)
    {
        if (pNode == null) return null;
        //当前节点右子树非空,则查找右子树的最左子节点
        if (pNode.right != null)
        {
            return rightIsNotNull(pNode);
        } else if (pNode.parent != null)  //右子树为空,父节点非空
        {
            return rightIsNull(pNode);
        }
        return null;
    }

    //右子树非空
    private static TreeLinkNode rightIsNotNull(TreeLinkNode pNode)
    {
        TreeLinkNode right = pNode.right;
        //查找最左子节点
        while (right.left != null)
        {
            right = right.left;
        }
        return right;
    }

    //右子树为空,父节点非空
    private static TreeLinkNode rightIsNull(TreeLinkNode pNode)
    {
        TreeLinkNode current = pNode;
        TreeLinkNode parent = pNode.parent;
        //沿着指向父节点的指针一直向上遍历,直到找到一个节点current,
        //current是其父节点的左节点(current==parent.left),则退出循环
        while (parent != null && current == parent.right)
        {
            current = parent;
            parent = parent.parent;
        }
        return parent;
    }
}

class TreeLinkNode
{
    int val;
    TreeLinkNode left = null;
    TreeLinkNode right = null;
    TreeLinkNode parent = null; //父节点

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324782922&siteId=291194637