Minimalism python solution 7 lines


  • 0
    H
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def constructMaximumBinaryTree(self, nums):
            """
            :type nums: List[int]
            :rtype: TreeNode
            """
            if len(nums) <= 0:
                return None
            root = TreeNode(max(nums))
            maxIndex = nums.index(root.val)
            root.left = self.constructMaximumBinaryTree(nums[:maxIndex])
            root.right = self.constructMaximumBinaryTree(nums[maxIndex+1:])
            return root
    

Log in to reply
 

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