Simple python in-place recursive solution - 92ms


  • 0
    S
    class Solution:
        # @param {integer[]} nums
        # @return {TreeNode}
        def sortedArrayToBST(self, nums):
            return self.sortedArrayToBSTinplace(nums, 0, len(nums))
    
        def sortedArrayToBSTinplace(self, nums, start, end):
            if end - start == 1: return TreeNode(nums[start:end][0])
            if end - start == 0: return None
    
            mid = (start + end) - 1 >> 1
    
            root = TreeNode(nums[mid])
            
            root.left = self.sortedArrayToBSTinplace(nums, start, mid)
            root.right = self.sortedArrayToBSTinplace(nums, mid+1, end)
            
            return root

Log in to reply
 

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