106. 从中序与后序遍历序列构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:
3
/ \
9 20
/ \
15 7
来源:力扣(LeetCode)
# 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: List[int], postorder: List[int]) -> TreeNode: def dfs(inorder,in_start,in_end,postorder,post_start,post_end): if in_start>in_end:return None if in_start==in_end:return TreeNode(inorder[in_start]) if post_start==post_end:return TreeNode(inorder[in_start]) root=TreeNode(postorder[post_end]) i=inorder.index(root.val) root.left=dfs(inorder,in_start,i-1,postorder,post_start,post_start+i-1-in_start) root.right=dfs(inorder,i+1,in_end,postorder,post_start+i-in_start,post_end-1) return root n=len(inorder) return dfs(inorder,0,n-1,postorder,0,n-1)