leetCode_105 Constructs a binary tree based on the pre-order and middle-order traversal of a tree

topic:

The binary tree is constructed according to the pre-order and middle-order traversal of a tree.

Note:
You can assume that there are no duplicate elements in the tree.

For example, given

Preorder traversal preorder = [3,9,20,15,7] Inorder
traversal inorder = [9,3,15,20,7]
returns the following binary tree:

    3
   / \
  9  20
    /  \
   15   7

Analysis method: divide and conquer

Combine the definitions of "pre-order traversal" and "middle-order traversal"

The first node of the pre-order traversal must be the root node of the binary tree;

The root node of the in-order traversal divides the in-order traversal into two parts. The left part constitutes the left subtree of the root node of the binary tree, and the right part constitutes the right subtree of the root node of the binary tree.

Determine 3 as the root node. According to the in-order traversal, you can know that 3 is the left subtree of the root node on the left, and the right subtree of the root node on the right; then recursion...

Next, I will repeat the ideas

Suppose preLeft is the left boundary of pre-order traversal, preRight is the right boundary of pre-order traversal; inLeft is the left boundary of middle-order traversal, and inRight is the right boundary of middle-order traversal.

Then preLeft points to the root node root, the left boundary of the left subtree of the root node is preLeft+1, and the root node can be found in the middle-order traversal. Set pIndex to point to the middle-order traversal root node, then the middle-order traverse the left child of the middle root node The interval of the tree is [inLeft, pIndex-1], and the interval of the right subtree is [pIndex+1, inRight]. What is left is that the preorder traverses the right boundary of the left subtree and the left boundary of the right subtree, but the length of the left subtree is the same as the length of the middle order traversal, which can be obtained by the following relationship:

The final result is as follows

Write code:

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }

    }


    public static TreeNode buildTree(int[] preorder, int[] inorder) {
        int preLen = preorder.length;//前序遍历的长度
        int inLen = inorder.length;//中序遍历的长度

        // 可以不做判断,因为题目中给出的数据都是有效的
        if (preLen != inLen) {
            return null;
        }

        // 以空间换时间,否则,找根结点在中序遍历中的位置需要遍历
        Map<Integer, Integer> map = new HashMap<>(inLen);
        for (int i = 0; i < inLen; i++) {
            map.put(inorder[i], i);
        }

        return buildTree(preorder,0, preLen - 1,map, 0, inLen - 1);
    }

    /**
     * 根据前序遍历数组的 [preL, preR] 和 中序遍历数组的 [inL, inR] 重新组建二叉树
     *
     *
     * @param preorder
     * @param preL 前序遍历数组的区间左端点
     * @param preR 前序遍历数组的区间右端点
     * @param map
     * @param inL  中序遍历数组的区间左端点
     * @param inR  中序遍历数组的区间右端点
     * @return 构建的新二叉树的根结点
     */
    private static TreeNode buildTree(int[] preorder, int preL, int preR,
                                      Map<Integer, Integer> map, int inL, int inR) {
        if (preL > preR || inL > inR) {
            return null;
        }
        // 构建的新二叉树的根结点一定是前序遍历数组的第 1 个元素
        int rootVal = preorder[preL];
        TreeNode root = new TreeNode(rootVal);

        int pIndex = map.get(rootVal );//中序遍历根节点的索引

        // 按照图中描述,计算边界的取值
        root.left = buildTree(preorder, preL + 1, preL + (pIndex - inL), map, inL, pIndex - 1);
        root.right = buildTree(preorder, preL + (pIndex - inL) + 1, preR, map, pIndex + 1, inR);
        return root;
    }

This is the end!

 

Source: LeetCode
Link: https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/weixin_43419256/article/details/107832473