Pointing to the Offer-Question 7 (Java Edition): Reconstructing the Binary Tree

Reference from: "Sword Pointing Offer - Famous Enterprise Interviewer Talking About Typical Programming Questions"
and blog: https://www.cnblogs.com/edisonchou/p/4741099.html

Topic : Reconstructing a binary tree
Enter the results of preorder traversal and inorder traversal of a binary tree, and please reconstruct the binary tree. It is assumed that the results of the input preorder traversal and inorder traversal do not contain duplicate numbers. For example, input the preorder traversal sequence {1, 2, 4, 7, 3, 5, 6, 8} and the inorder traversal sequence {4, 7, 2, 1, 5, 3, 8, 6}, then reconstruct the binary tree and output its head node.

Main idea : In the preorder traversal sequence of a binary tree, the first number is the value of the root node of the tree. But in an inorder traversal sequence, the value of the root node is in the middle of the sequence, the value of the node of the left subtree is located to the left of the value of the root node, and the value of the node of the right subtree is located in the value of the root node. to the right. Therefore, by scanning the in-order traversal sequence, the position of the root node is found, and then the left and right subtrees of the root node are divided. Finally, the left and right subtrees are reconstructed recursively, respectively.

E.g:

            1
         /     \
        2       3
       /       / \
      4       5   6
       \         /
        7       8

Preorder traversal sequence: { 1, 2, 4, 7, 3, 5, 6, 8 }
Inorder traversal sequence: { 4, 7, 2, 1, 5, 3, 8, 6 }

root node: 1

Left subtree preorder traversal sequence: 2, 4, 7
Left subtree inorder traversal sequence: 4, 7, 2

Right subtree preorder traversal sequence: 3, 5, 6, 8
Right subtree inorder traversal sequence: 5, 3, 8, 6

Then recursively rebuild the left and right subtrees, respectively.

Key point : Preorder traversal Features: The first element is the root node. In-order traversal features: the left subtree of the root node is the left subtree, and the right subtree is the right subtree. recursive.

Time complexity: O (tree length), Note: Time is mainly spent in finding the root node position

public class ReConstructBinaryTree
{
    public static void main(String[] args)
    {
        int[] preOrder = {1, 2, 4, 7, 3, 5, 6, 8};
        int[] inOrder = {4, 7, 2, 1, 5, 3, 8, 6};
//            1
//         /    \
//        2      3
//       /      / \
//      4      5   6
//       \        /
//       7       8
        TreeNode result = reconstructBinaryTree(preOrder, inOrder);
    }

    public static TreeNode reconstructBinaryTree(int[] preOrder, int[] inOrder)
    {
        if (preOrder == null || inOrder == null) return null;
        return rebuild(preOrder, inOrder, 0, preOrder.length - 1, 0, inOrder.length - 1);
    }

    /**
     * @param preOrder 前序遍历序列
     * @param inOrder  中序遍历序列
     * @param preStart 前序遍历开始位置
     * @param preEnd   前序遍历结束位置
     * @param inStart  中序遍历开始位置
     * @param inEnd    中序遍历结束位置
     * @return
     */
    public static TreeNode rebuild(int[] preOrder, int[] inOrder, int preStart, int preEnd,
                                   int inStart, int inEnd)
    {
        if (preStart > preEnd) return null;
        //前序遍历第一个元素就是根节点
        int rootVal = preOrder[preStart];
        int inRootIndex = inStart;  //中序遍历中的根节点位置
        while (inRootIndex <= inEnd)
        {
            //在中序遍历中查找根节点位置
            if (rootVal == inOrder[inRootIndex]) break;
            else inRootIndex++;
        }
        int leftLength = inRootIndex - inStart;  //左子树长度
        TreeNode rootNode = new TreeNode(rootVal); //创建根节点
        int preLeftEnd = preStart + leftLength;
        //创建左子树
        rootNode.left = rebuild(preOrder, inOrder, preStart + 1, preLeftEnd,
                inStart, inRootIndex - 1);
        //创建右子树
        rootNode.right = rebuild(preOrder, inOrder, preLeftEnd + 1, preEnd,
                inRootIndex + 1, inEnd);
        return rootNode;
    }
}

Guess you like

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