# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # 返回构造的TreeNode根节点 def reConstructBinaryTree(self, pre, tin): # write code here if (not pre) or (not tin) or (len(pre) != len(tin)): return None # 去除pre的值 root = pre[0] rootNode = TreeNode(root) position = tin.index(root) tinleft = tin[:position] tinright = tin[position+1:] preleft = pre[1:position+1] preright = pre[position+1:] leftNode = self.reConstructBinaryTree(preleft, tinleft) rightNode = self.reConstructBinaryTree(preright, tinright) rootNode.left = leftNode rootNode.right = rightNode return rootNode