class Solution(object):
def buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""
if not preorder:return
curr=TreeNode(preorder[0])
index=inorder.index(preorder[0])
curr.left=self.buildTree(preorder[1:index+1],inorder[:index])
curr.right=self.buildTree(preorder[index+1:],inorder[index+1:])
return curr
What's wrong with my python code , result is MLE


I was using a helper function like
def buildTreeHelper(self, preorder, i, j, inorder, m, n):
where preorder[i:j] would be equivalent to preorder[1:index+1] in your solution and similarly inorder[m:n] would be equivalent to inorder[:index]. In this way, each recursive call uses the original lists and takes the indices as the boundaries.The trick part is to figure out the indices that need to be passed in.
Let me know if you have further questions.