654. Maximum Binary Tree
Python
from typing import List, Optional
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 not nums:
return
max_val = max(nums)
index_max = nums.index(max_val)
return TreeNode(
val=max_val,
left=self.constructMaximumBinaryTree(nums[:index_max]),
right=self.constructMaximumBinaryTree(nums[index_max + 1:])
)