# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pre int整型一维数组 # @param vin int整型一维数组 # @return TreeNode类 # class Solution: def reConstructBinaryTree(self , pre: List[int], vin: List[int]) -> TreeNode: # write code here def build(preStart:int, preEnd:int, vinStart:int, vinEnd:int): if preStart > preEnd: return None rootIndex = preStart vinroot = index[pre[rootIndex]] root = TreeNode(pre[rootIndex]) leftSize = vinroot - vinStart root.left = build(preStart+1, preStart+leftSize, vinStart, vinroot-1) root.right = build(preStart+leftSize+1, preEnd, vinroot+1, vinEnd) return root index = {element:i for i, element in enumerate(vin)} n = len(pre) return build(0, n-1, 0, n-1)