4. Reconstruction of binary tree, topic analysis and java implementation of sword finger offer

Title Description

Enter the result of the preorder traversal and inorder traversal of a binary tree, and rebuild the binary tree. It is assumed that the results of the input preorder traversal and preorder traversal do not contain duplicate numbers. For example, if the sequence {1,2,4,7,3,5,6,8} and the sequence {4,7,2,1,5,3,8,6} are input, the binary tree will be reconstructed and returned.

Requirement

Time limit: 1 second space limit: 32768K

Parameter type

 //Binary tree node
 public class TreeNode {
     int val;
     TreeNode left;//Left tree
     TreeNode right;//Right subtree
     TreeNode(int x) { val = x; }
 }

Implementation ideas

According to the description of the topic, the input parameters of the function are the pre order traversal and the subsequent traversal of the original binary tree. According to the knowledge of binary tree, the first element of pre order traversal sequence is the root node. After getting the root node, do the following steps in turn

  1. Traversing the middle order traversal sequence to obtain the position of the root node in the sequence
  2. After the first step, you can get the number of nodes in the left subtree and the middle order traversal sequence of the left subtree (the same as the right subtree)
  3. Traversing the preorder traversal sequence, obtaining the preorder traversal sequence of left subtree and right subtree
  4. Passing in the pre middle order traversal sequence of left and right subtrees, recursively building the left and right subtrees

code implementation

public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
    TreeNode root=new TreeNode(pre[0]);//The first number of preorder traversal must be the root node  
    int len=pre.length;  
    //When there is only one number to traverse the current order, it means that the node is a leaf node
    if(len==1){  
        root.left=null;  
        root.right=null;  
        return root;  
    }  
    int rootvalue=root.val;  //rootval is the value of the root node
    int i;  
    //Find the root node position in the middle order traversal, and assign it as i  
    for(i=0;i<len;i++){  
        if(rootvalue==in[i])  
            break;  
    }  
    //Create left subtree  
    if(i>0){  
        int[] pr=new int[i];  
        int[] ino=new int[i];  
        for(int j=0;j<i;j++){  
            //Get the previous traversal sequence of left subtree
            pr[j]=pre[j+1];  
            //Get the middle order traversal sequence of left subtree
            ino[j]=in[j]; 
        }  
        //Recursively building the left subtree
        root.left=reConstructBinaryTree(pr,ino);  
    }else{  
        root.left=null;  
    }  
    //Create right subtree  
    if(len-i-1>0){  
        int[] pr=new int[len-i-1];  
        int[] ino=new int[len-i-1];  
        for(int j=i+1;j<len;j++){  
            ino[j-i-1]=in[j];  
            pr[j-i-1]=pre[j];  
        }  
        //Recursively building right subtree
        root.right=reConstructBinaryTree(pr,ino);  
    }else{  
        root.right=null;  
    }  
    return root;
    }

Posted by kooks on Tue, 31 Mar 2020 17:50:23 -0700