当前位置:网站首页>Sword finger offer 07 Rebuild binary tree

Sword finger offer 07 Rebuild binary tree

2022-07-05 04:17:00 xzystart

The finger of the sword Offer 07. Reconstruction of binary tree
Enter the results of preorder traversal and inorder traversal of a binary tree , Build the binary tree and return its root node .

It is assumed that the results of the input preorder traversal and the middle order traversal do not contain repeated numbers .

Example 1:

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
Example 2:

Input: preorder = [-1], inorder = [-1]
Output: [-1]

Limit :

0 <= Number of nodes <= 5000

class Solution {
    
    int[] preorder; // Preorder traversal array 
    HashMap<Integer, Integer> map = new HashMap<>();   // Middle order traversal table mapping   The key traverses the value of the array in the preceding order , It's worth it inorder Position in array 
    public TreeNode buildTree(int[] preorder, int[] inorder) {
    
            // According to the previous traversal, we can get that the root node is the first element of the array 
            // According to the root node, you can get it into the array , The left of the root node is the left subtree , On the right of the root node is the right subtree 
        
        this.preorder = preorder;
        for(int i = 0 ;i<preorder.length;i++){
    
            // Save mapping 
            map.put(inorder[i],i);
        }
        return helper(0,inorder.length-1,0);
    }
 private TreeNode helper(int left,int right,int root){
    // among root by preorder Position of the first element in the array  ,
                                                        // The left and right pointers point to inorder The left and right bounds of the array 
    // Exit conditions ( When the left and right pointers point to the same position , It proves that he is a leaf node , There are no nodes below )
    if(left>right) return null;
    // Create a header node 
    TreeNode head = new TreeNode(preorder[root]);
    // Get the subscript of the corresponding element of the end node in the ordered array 
    int n = map.get(preorder[root]);
    // The index of the root of the left subtree is the root node in the preorder +1 
    // The left boundary of the recursive left subtree is the original middle order in_left
    // The right boundary of the recursive left subtree is the index of the root node in the middle order -1
    head.left = helper(left,n-1,root+1);
    // The index of the root of the right subtree is   The current root position  +  The number of left subtrees  + 1
    // The left boundary of the recursive right subtree is the current root node in the middle order +1
    // The right boundary of the recursive right subtree is the boundary of the original right subtree in the middle order 
    head.right = helper(n+1,right,root+1+(n-left));
 head.right = helper(n+1,right,root+1+(n-left));
   // n - left + root + 1  Means   Root node index  +  Left subtree length  + 1
   //Input: preorder = [3,9,20,15,7],  Here, the root is 3 when , The length of the left subtree is 1, The calculation method is inorder = [9,3,15,20,7] in 3 Index subscript of - Left boundary 
    return head;
 } 
}
原网站

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