'''
根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
根据后序遍历的最后一个节点的数值得出根节点,然后在中序遍历的序列中找到根节点的索引下标,
将中序遍历的序列切分成左子树和右子树,递归产生叶子节点
'''
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def buildTree(self, inorder, postorder):
if len(inorder)==0:
return None
if len(inorder)==1:
return TreeNode(inorder[0])
root_vaule=postorder.pop(-1)
index=inorder.index(root_vaule)
root=TreeNode(root_vaule)
root.left=self.buildTree(inorder[:index],postorder[:index])
root.right=self.buildTree(inorder[(index+1):],postorder[index:])
return root