Why this python solution fails only test case 32?


  • 0
    Z

    I don't understand why my method is not passing test case 32?

    def sortedArrayToBST(self, nums):
            """
            :type nums: List[int]
            :rtype: TreeNode
            """
            if nums:
                n = len(nums)
                count = 0
                return self.toBSTHelper(nums, 0, n-1 ,count)
            else:
                return None
            
        def toBSTHelper(self, nums, i, j, count):
            count += 1
            if count == 10:
                return None
            if i== j:
                return TreeNode(nums[j])
            elif i > j:
                return None
            mid = i+((j-i)//2)
            root = TreeNode(nums[mid])
            root.left = self.toBSTHelper(nums, i, mid-1, count)
            root.right = self.toBSTHelper(nums, mid+1,j, count)
            
            return root
    

Log in to reply
 

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