class TreeNode(object):
def __init__(self,x):
self.val=x
self.left=None
self.right=None
class Solution(object):
def buildTree(self,preorder,inorder):
if not preorder:return None
idx=inorder.index(preorder[0])
root=TreeNode(preorder[0])
l=self.buildTree(preorder[1:idx+1],inorder[:idx])
r=self.buildTree(preorder[idx+1:],inorder[idx+1:])
root.left=l
root.right=r
return root
哇啊啊啊我好菜都没想到用hash table,
可能是切片用的太顺手了