Can someone help to check why I got "maximum recursion depth exceeded" with those codes in Python


  • 0
    L

    It is a typical and easy recursive solution in Python. I did some simple codes but couldn't figure how "maximum recursion depth exceeded
    " message is got. I saw very similar codes in the forum and they can run well. Please help to check why my codes are with bug. Thanks.

    
    class Solution(object):
            def sortedArrayToBST(self, nums):
                    """
                    :type nums: List[int]
                    :rtype: TreeNode
                    """
                    if not nums: return None
                    tree = self.setSubTree(nums, 0, len(nums)-1)
                    return tree
    
            def setSubTree(self, nums, start, end):
                    root_pos = (start+end) // 2
                    root = TreeNode(nums[root_pos])
                    if root_pos is not start: root.left  = self.setSubTree(nums, start, root_pos-1)
                    if root_pos is not end:   root.right = self.setSubTree(nums, root_pos+1, end)
                    return root
    

Log in to reply
 

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