654. Maximum Binary Tree
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 12, 2024
Last updated : July 01, 2024
Related Topics : Array, Divide and Conquer, Stack, Tree, Monotonic Stack, Binary Tree
Acceptance Rate : 85.77 %
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
if len(nums) == 0 :
return None
maxx = max(nums)
return TreeNode(maxx,
self.constructMaximumBinaryTree(nums[:nums.index(maxx)]),
self.constructMaximumBinaryTree(nums[nums.index(maxx) + 1:]))