Python 7 lines solution


  • 0
    J
    # 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 not nums:
                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.