# Sword finger Offer 07. Recursive method of reconstructing binary tree

#### Sword finger Offer 07. Rebuild binary tree

Enter the results of preorder traversal and inorder traversal of a binary tree, please build the binary tree and return its root node.

It is assumed that the input pre order traversal and middle order traversal results do not contain duplicate 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]
```

I believe most children's shoes have done this problem, but it is usually in the form of selection or filling in the blank. It may be a little confused to suddenly use a language to realize it.

First, we can review how pre sequence and middle sequence are formed:

Front sequence: root node – > left subtree -- > right subtree

Middle sequence: left subtree -- > root node -- > right subtree

Method 1: recursion

So we first find the first element preorder from the preorder sequence_ Root is the root element of the whole tree

Then locate the inorder of the root element in the middle order sequence_ root，

To the left of this position is the left subtree (0 ~ inorder_root-1)

On the right is the right subtree (inorder_root+1 ~ n-1).

We can judge the number of elements in the left subtree (size_left_subtree) by the position of the root element in the middle order sequence

By the number of elements in the left subtree, we can locate them in the preorder sequence

Left subtree (preorder_root+1 ~ + preorder+size_left_subtree)

and

Right subtree (preorder+size_left_subtree~ n)

Next, we need to recursively establish the root node and connect each node with its left and right child nodes

It should be noted here that in order to locate the position of the root element in the middle order sequence, we create a HashMap for it. The key is the value of the current element and the value is the index of the current element (this method is available because the title indicates that there are no duplicate elements in the sequence)

The code is as follows:

```/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
private HashMap<Integer,Integer> indexMap;
public TreeNode buildTree(int[] preorder, int[] inorder) {
int n = preorder.length;
//Create HashMap
indexMap = new HashMap<Integer,Integer>();
//Initialize HashMap
for(int i=0; i<n; i++){
indexMap.put(inorder[i], i);
}
return myBuildTree(preorder, inorder, 0, n-1, 0, n-1);
}

public TreeNode myBuildTree(int[] preorder, int[] inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right){
// Recursive boundary conditions
if(preorder_left > preorder_right){return null;}
// The first node in the previous sequence is the root node
int preorder_root = preorder_left;
// Find the corresponding node serial number in the middle order
int inorder_root = indexMap.get(preorder[preorder_root]);
// Establish root node
TreeNode root = new TreeNode(inorder[inorder_root]);
// Get the number of nodes in the left subtree
int size_left_subtree = inorder_root - inorder_left;
// Recursively traverse the left subtree and connect to the root node
// Preorder of preorder traversal_ Size from left + 1_ left_ The subtree node is the 0th to inorder of the middle order traversal_ Root-1 nodes
root.left = myBuildTree(preorder, inorder, preorder_left+1, preorder_left+size_left_subtree, inorder_left, inorder_root-1);
//Recursively traverse the right subtree and connect to the root node
// Preorder of preorder traversal_ left+1+size_ Left + subtree to preorder_right is the inorder that must be traversed_ Root + 1 to inorder_right node
root.right = myBuildTree(preorder, inorder, preorder_left+size_left_subtree+1, preorder_right, inorder_root+1, inorder_right);
return root;
}
}
```

Posted on Mon, 08 Nov 2021 00:22:19 -0500 by manojhooda