# 105. Construct Binary Tree from Preorder and Inorder Traversal

## Problem Statement

<br>

Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return *the binary tree*.

&#x20;

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg)

<pre><code><strong>Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
</strong><strong>Output: [3,9,20,null,null,15,7]
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: preorder = [-1], inorder = [-1]
</strong><strong>Output: [-1]
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= preorder.length <= 3000`
* `inorder.length == preorder.length`
* `-3000 <= preorder[i], inorder[i] <= 3000`
* `preorder` and `inorder` consist of **unique** values.
* Each value of `inorder` also appears in `preorder`.
* `preorder` is **guaranteed** to be the preorder traversal of the tree.
* `inorder` is **guaranteed** to be the inorder traversal of the tree.

## Intuition

```
Approach:

Maintain two indexes in pre and inorder array
When start crosses end, We stop , elements over

Now What we do is we hash the inorder elements

And notice that

Inorder is Left Root Right
Preorder is Root Left Right

Now We shift the pointers accordingly

Like when we take preorder for next left sub-tree
We take preorder +1 to preorder+no_elements (between inorder start to index of root)

As all the preorder are in line
```

### Links

<https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/>

### Video Links

<https://www.youtube.com/watch?v=aZNaLrVebKQ&ab_channel=takeUforward>

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    unordered_map<int, int>mp;
    TreeNode* Tree(vector<int>& preorder, int pre_start, int pre_end, vector<int>& inorder, int in_start, int in_end){
        if(pre_start > pre_end or in_start > in_end)
            return nullptr;

        TreeNode *root = new TreeNode(preorder[pre_start]);
        int inRoot = mp[root->val];
        int numsLeft = inRoot-in_start;

        root->left = Tree(preorder, pre_start+1, pre_start+numsLeft, inorder, in_start, inRoot-1);
        root->right = Tree(preorder, pre_start+1+numsLeft, pre_end, inorder, inRoot+1, in_end);

        return root;
    }

    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        for(int i=0; i<inorder.size(); i++)
            mp[inorder[i]] = i;

        return Tree(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1);
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/trees/binary-tree/hard/105.-construct-binary-tree-from-preorder-and-inorder-traversal.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
