My python code, runtime beats 99.53%


  • 0
    6
    class Solution(object):
        def sortedArrayToBST(self, nums):
            """
            :type nums: List[int]
            :rtype: TreeNode
            """
            if not nums:
                return None
            length = len(nums)
            def recur(s, e, node=False):
                if s <= e:
                    mid = (s + e) / 2
                    node = TreeNode(nums[mid])
                    node.left = recur(s, mid - 1)
                    node.right = recur(mid + 1, e)
                    return node
                else:
                    return None
            root = recur(0, length - 1)
            return root
    

Log in to reply
 

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