# 从中序与后序遍历序列构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:
3
/ \
9 20
/ \
15 7
## template
```python
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
def build_tree(in_left, in_right, post_left, post_right):
if in_left > in_right:
return
post_root = post_right
root = TreeNode(postorder[post_root])
in_root = inorder_map[root.val]
size_of_left = in_root - in_left
root.left = build_tree(
in_left, in_root - 1, post_left, post_left + size_of_left - 1
)
root.right = build_tree(
in_root + 1, in_right, post_left + size_of_left, post_root - 1
)
return root
size = len(inorder)
inorder_map = {}
for i in range(size):
inorder_map[inorder[i]] = i
return build_tree(0, size - 1, 0, size - 1)
```
## 答案
```python
```
## 选项
### A
```python
```
### B
```python
```
### C
```python
```