Easy to understand python solution, recursively


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

Log in to reply
 

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