# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None def contree(pre,tin): if len(tin)==1 and len(pre)==1: return TreeNode(tin[0]) if pre==[] and tin==[]: return None for i in range(0,len(tin)): if tin[i]==pre[0]: print pre[1:1+i] print pre[1+i:] lt=contree(pre[1:1+i],tin[:i]) rt=contree(pre[i+1:],tin[i+1:]) tempnode=TreeNode(tin[i]) tempnode.left=lt tempnode.right=rt return tempnode class Solution: # 返回构造的TreeNode根节点 def reConstructBinaryTree(self, pre, tin): # write code here return contree(pre,tin)
原文:https://www.cnblogs.com/zealousness/p/10355688.html