当前位置:网站首页>Rebuild binary tree

Rebuild binary tree

2022-06-22 06:42:00 FIappy Brid

Title Description

 Insert picture description here

import java.util.*;
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
public class Solution {
    
    //  The former sequence traversal :  root   Left   Right 
    //  In the sequence traversal :  Left   root   Right 
    public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
    
        //  Through the preface   And middle order traversal   Restore a binary tree !
        //  Recursive version  __>  Optimize , Is to store the subscript and value in the hash table , It will save more time to find the location !
        //return helper(pre,vin,0,pre.length-1,0,pre.length - 1);
        
        //  Let's start the non recursive version 
        //  Traverse each element in the array traversed by the preceding sequence , Take each element as the root , Get their left and right subtrees ;
        
    if (pre == null || pre.length == 0) {
    
            return null;
        }
        int mid_index = 0;
        TreeNode root = new TreeNode(pre[0]); 
        Deque<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        //  stay  vin  in  [ The left subtree ] [ Right subtree ]  root 
        //  Sequence traversal results before sequence traversal !
         //  root  [ The left subtree ][ Right subtree ]
        for(int i = 1; i < pre.length; i++){
    
            TreeNode cur = stack.peek();
            int preorder_val = pre[i];
            if(cur.val != vin[mid_index]){
     //  Until traversal  vin[mid_index]  Left subtree of root 
                cur.left = new TreeNode(preorder_val);
                stack.push(cur.left);
            }else {
    
                    while(!stack.isEmpty() && stack.peek().val == vin[mid_index]){
     
                        cur = stack.pop();
                        mid_index++;
                    } 
                    cur.right = new TreeNode(preorder_val);
                    stack.push(cur.right);
            }
        }
        return root;
    }
    //  With pre[l_l] Traversal interval of subtree as root [l_l,l_r]
    //  With pre[l_l] The interval of the subtree as the root in the middle order traversal [r_l,r_r];
    //  Through constant partition , Solve the problem   Finally, the solution of the small problem is merged   Get the solution of the big problem 
    private TreeNode helper(int[] pre,int[] vin,int l_l,int l_r,int r_l,int r_r){
    
        if(l_l > l_r) return null;
        TreeNode root = new TreeNode(pre[l_l]);
        //  Find the position of the root in the middle order traversal , Then again   subtract  r_l , The result is the width of the left subtree rooted at the current node 
        int index_in = index(vin,root.val);
        int wid = index_in - r_l; //  Find the interval length of the left subtree 
        //  Right subtree   Section   The interval of left subtree in preorder traversal  &  The middle order traverses the corresponding interval of the left subtree 
        root.left = helper(pre,vin,l_l + 1,l_l + wid,r_l,index_in - 1);
        						// The interval of right subtree subtree in preorder traversal  &  The middle order traverses the corresponding interval of the right subtree 
        root.right = helper(pre,vin,l_l + wid + 1,l_r,index_in+1,r_r);
        return root;
    }
    //  Traverse the position in the result set in the middle order  
    private int index(int[] vin,int val){
    
        int ans = -1;
        for(int i = 0; i < vin.length; i++){
    
            if(vin[i] == val){
    
                ans = i;
                break;
            }
        }
        return ans;
    }
}
原网站

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