My accepted python solution


  • 0
    J
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def sortedArrayToBST(self, nums):
            """
            :type nums: List[int]
            :rtype: TreeNode
            """
            n = len(nums)
            if not n:
                return None
            if n == 1:
                return TreeNode(nums[0])
            nodeVal = nums[(n-1)/2]
            hereNode = TreeNode(nodeVal)
            hereNode.left = self.sortedArrayToBST([i for i in nums if i<nodeVal])
            hereNode.right = self.sortedArrayToBST([i for i in nums if i>nodeVal])
            return hereNode
    

Log in to reply
 

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