My python code get a runtime error, WHY?


  • 0
    B
    class Solution:
        def buildTree(self, inorder, postorder):
            def generate_node(instart, inend, poststart, postend):
                if instart > inend or poststart > postend:
                    return None
                root = TreeNode(postorder[postend])
                idx = 0
                for x in xrange(instart, inend + 1):
                    if inorder[x] == postorder[postend]:
                        idx = x
                        break
                root.left = generate_node(
                    instart, idx - 1, poststart, poststart + idx - 1 - instart)
                root.right = generate_node(
                    idx + 1, inend, postend - inend + idx, postend - 1)
                return root
            return generate_node(0, len(inorder)-1, 0, len(postorder)-1)

  • 0
    Y

    It exceeds the maximum recursion depth on some extreme cases like all the nodes only have left children.


  • 0
    B

    yes, I think so. Thank you for your answer.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.