WechatIMG25.jpeg这个题没啥好说的 easy
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
if not preorder or not inorder:
return None
val = preorder[0]
root = TreeNode(val)
index = inorder.index(val)
left = self.buildTree(preorder[1:index+1], inorder[:index])
right = self.buildTree(preorder[index+1:], inorder[index+1:])
root.left = left
root.right = right
return root
网友评论