# -*- 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 #pre是前序遍历 tin是中序遍历 if not pre or not tin: return None root = TreeNode(pre[0]) index = tin.index(root.val) root.left = self.reConstructBinaryTree(pre[1:index+1],tin[:index]) root.right = self.reConstructBinaryTree(pre[index+1:],tin[index+1:]) return root
原文:https://www.cnblogs.com/ansang/p/11892350.html