当前位置:网站首页>Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]

Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]

2022-06-24 14:06:00 REN_ Linsen

Preface

The generation of binary tree , Generally speaking, it is analyzed from top to bottom, that is, divided into , Splicing subtrees from bottom to top , Finally, we get the whole binary tree . Examine the division of left and right subtrees + The left and right subtrees trace back and splice knowledge points .

One 、 According to the preface & Middle order traversal generates binary tree

 Insert picture description here

Two 、 Recursive partition + Backtracking splicing subtree

package everyday.medium;

import java.util.HashMap;
import java.util.Map;

//  Using preorder and inorder traversal to construct binary tree .
public class BuildTree {
    
    /* target: Using preorder and inorder traversal to construct binary tree .  Antecedent function ? Get all the root nodes on the left subtree in turn .  Middle order function ? The left and right subtrees are divided by the root node traversed by the previous order , Continuous division , Generate a binary tree from bottom to top . */
    public TreeNode buildTree(int[] preorder, int[] inorder) {
    
        // assert  No repeating elements .
        Map<Integer, Integer> m = new HashMap<>();
        //  preparation , Get the root node location quickly , Divide the left and right subtrees .
        for (int i = 0; i < inorder.length; i++) m.put(inorder[i], i);
        //  Building tree 
        return buildTree(preorder, inorder, 0, inorder.length - 1, m);
    }

    int idx = 0;

    private TreeNode buildTree(int[] preorder, int[] inorder, int begin, int end, Map<Integer, Integer> m) {
    
        if (begin > end) return null;
        //  Use the root node to divide the left and right subtrees , Root node by preorder[idx] Come to .
        int mid = m.get(preorder[idx++]);
        //  Get recursively generated left and right children .
        TreeNode left = buildTree(preorder, inorder, begin, mid - 1, m);
        TreeNode right = buildTree(preorder, inorder, mid + 1, end, m);
        //  Generate root node .
        TreeNode root = new TreeNode(inorder[mid]);
        //  Join the left and right subtrees .
        root.left = left;
        root.right = right;
        //  Back to the spliced root Trees .
        return root;
    }

    // Definition for a binary tree node.
    public class TreeNode {
    
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
    
        }

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

        TreeNode(int val, TreeNode left, TreeNode right) {
    
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}

summary

1) The generation of binary tree , Generally speaking, it is analyzed from top to bottom, that is, divided into , Splicing subtrees from bottom to top , Finally, we get the whole binary tree .
2) The preorder traversal function recursively determines the root nodes of the left and right subtrees in turn , In the sequence traversal + The root node can divide the left and right subtrees .

reference

[1] LeetCode According to the preface & Middle order traversal generates binary tree

原网站

版权声明
本文为[REN_ Linsen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241152225239.html