f********a 发帖数: 165 | 1 class Solution:
# @param inorder, a list of integers
# @param postorder, a list of integers
# @return a tree node
def buildTree(self, inorder, postorder):
n = len(inorder)
if n == 0:
return None
elif n == 1:
return TreeNode(postorder[-1])
else:
root = TreeNode(postorder[-1])
mid_inorder = inorder.index(postorder[-1])
root.left = self.buildTree(inorder[:mid_inorder], postorder[:mid
_inorder])
root.right = self.buildTree(inorder[mid_inorder+1:], postorder[
mid_inorder:-1])
return root
是因为inorder[:mid_inorder]重新拷贝了一份substring所以递归就超内存了? |
|