leetcode_construct binary tree from inorder and postorder traversal

Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.
(根据中序遍历和后序遍历构建二叉树)

Example:

1. 递归

递归构建二叉树,首先根据后序遍历确定根节点,在根据中序遍历定位到根节点。其中,中序遍历根节点左边的为左子树,右边的为右子树。具体实现过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class :
def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
if not inorder:
return None

root = TreeNode(postorder[-1])
index = inorder.index(postorder[-1])

root.left = self.buildTree(inorder[:index], postorder[:index])
root.right = self.buildTree(inorder[index+1:], postorder[index:-1])

return root