Skip to content

Latest commit

 

History

History
43 lines (32 loc) · 1.27 KB

_654. Maximum Binary Tree.md

File metadata and controls

43 lines (32 loc) · 1.27 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


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 %


Solutions

Python

# 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:]))