construct bst from post and inorder 总是Memory Limit Exceeded# JobHunting - 待字闺中
f*a
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所以递归就超内存了?
# @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所以递归就超内存了?