Skip to content

Practice Maximum Binary Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#654
LeetCode Problem

Maximum Binary Tree

Problem Statement

Build a binary tree from the distinct values in nums. For each nonempty subarray, use its maximum value as the root, recursively build the left subtree from the values before that maximum, and build the right subtree from the values after it. An empty subarray produces no node. Return the resulting tree.

Source: Maximum Binary Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [3,2,1,6,0,5]
Output
[6,3,5,null,2,0,null,null,1]

# Example 2

Input
nums = [3,2,1]
Output
[3,null,2,null,1]

Constraints

1 <= nums.length <= 1000
0 <= nums[i] <= 1000
All integers in nums are unique.

Related LeetCode Problems

Practice more problems similar to Maximum Binary Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.