Python 7-line solution


  • 1
    X
    class Solution(object):
        def sortedArrayToBST(self, nums):
            if not nums:
                return None
            middle = len(nums) / 2
            root = TreeNode(nums[middle])
            root.left = self.sortedArrayToBST(nums[:middle])
            root.right = self.sortedArrayToBST(nums[middle+1:])
            return root

Log in to reply
 

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