# 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

        ret = TreeNode(max(nums))
        i = nums.index(ret.val)
        ret.left = self.constructMaximumBinaryTree(nums[:i])
        ret.right = self.constructMaximumBinaryTree(nums[i + 1:] if len(nums) > i + 1 else None)
        return ret
        

 

相关文章:

  • 2021-10-09
  • 2021-05-22
  • 2021-04-12
  • 2021-12-30
  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2021-06-21
猜你喜欢
  • 2021-09-24
  • 2021-12-10
  • 2021-07-05
  • 2022-12-23
  • 2022-01-21
  • 2021-10-01
  • 2021-12-23
相关资源
相似解决方案