---
# System prepended metadata

title: 105. Construct Binary Tree from Preorder and Inorder Traversal
tags: [' bt', ' tree', array]

---

---
link: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
tags: array, tree, bt
---

# 105. Construct Binary Tree from Preorder and Inorder Traversal

## Question
Given preorder and inorder traversal of a tree, construct the binary tree.

**Note:**
You may assume that duplicates do not exist in the tree.

For example, given

```
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
```

Return the following binary tree:

```
    3
   / \
  9  20
    /  \
   15   7
```

## Solution: Python
```python=
"""
O(n^2) runtime
"""

class Solution(object):
    def helper(self, preorder, inorder):
        """
        :type preorder: List[int]
        :type inorder: List[int]
        :rtype: TreeNode
        """
        if not inorder:
            return None
        
        root = preorder.index(preorder.pop(0))
        node = TreeNode(inorder[root])
        node.left = self.buildTree(preorder, inorder[:root])
        node.right= self.buildTree(preorder, inorder[root + 1:])
        
        return node
```

## Solution: Java
```java=        
```