Smallest and fastest Python solution. 147 ms


  • 7
    S
    class Solution:
    
        def sortedArrayToBST(self, num):
    
            if not num:
                return None
    
            median  = len(num)/2
            new_node = TreeNode(num[median])
    
            new_node.left = self.sortedArrayToBST(num[:median])
            new_node.right = self.sortedArrayToBST(num[median+1:])
            
            return new_node

Log in to reply
 

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