Accepted simple recursive Python Code


  • 0
    J
    class Solution(object):
        def sortedArrayToBST(self, nums):
            """
            :type nums: List[int]
            :rtype: TreeNode
            """
            if len(nums)==0:
                return None
            elif len(nums)==1:
                return TreeNode(nums[0])
            else:
    
                mid = len(nums)//2
    
                root = TreeNode(nums[mid])
                root.left = self.sortedArrayToBST(nums[0:mid])
                root.right = self.sortedArrayToBST(nums[mid+1:len(nums)])
    
                return root
    

Log in to reply
 

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