python - max binary tree


  • 0
    S
    # 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
            """
            ret = None
            
            if nums:
                maxn = max(nums)
                ret = root = TreeNode(maxn)
                root.left = self.constructMaximumBinaryTree(nums[:nums.index(maxn)])
                root.right = self.constructMaximumBinaryTree(nums[nums.index(maxn)+1:])
            
            return ret
    

Log in to reply
 

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