Imagine you're an architect designing a skyline where each building's height represents a value in an array. Your task is to construct a Maximum Binary Tree using a special recursive algorithm that creates the most prominent structure possible.

Given an integer array nums with no duplicates, build a maximum binary tree following these rules:

  1. Find the Maximum: Create a root node with the maximum value in the current array
  2. Left Subtree: Recursively build the left subtree using all elements to the left of the maximum
  3. Right Subtree: Recursively build the right subtree using all elements to the right of the maximum

For example, with [3,2,1,6,0,5], the maximum is 6 at index 3. So 6 becomes the root, [3,2,1] forms the left subtree, and [0,5] forms the right subtree. This process continues recursively until every element finds its place in this hierarchical structure.

Goal: Return the root of the maximum binary tree built from nums.

Input & Output

example_1.py โ€” Basic Case
$ Input: [3,2,1,6,0,5]
โ€บ Output: [6,3,5,null,2,0,null,null,1]
๐Ÿ’ก Note: Maximum 6 becomes root. [3,2,1] forms left subtree with 3 as root, 2 as right child, 1 as right child of 2. [0,5] forms right subtree with 5 as root, 0 as left child.
example_2.py โ€” Larger Array
$ Input: [3,2,1,6,0,5,4]
โ€บ Output: [6,3,5,null,2,0,4,null,1]
๐Ÿ’ก Note: Same as example 1, but 4 becomes right child of 5 since it comes after 5 in the array and 5 is the maximum in [0,5,4].
example_3.py โ€” Single Element
$ Input: [1]
โ€บ Output: [1]
๐Ÿ’ก Note: Edge case with single element - it becomes the root with no children.

Constraints

  • 1 โ‰ค nums.length โ‰ค 1000
  • 0 โ‰ค nums[i] โ‰ค 1000
  • All integers in nums are unique

Visualization

Tap to expand
Maximum Binary Tree Construction ProcessOriginal Array:3216056ROOT (Maximum)3Left Subtree Max5Right Subtree Max201๐ŸŽฏ Key Insight: Monotonic Stack SolutionEach element in the stack can 'see' all smaller elements to its right.When a larger element arrives, it becomes the parent of all smaller elements it pops.This naturally creates the maximum binary tree structure in O(n) time!
Understanding the Visualization
1
Find the CEO
Scan the array [3,2,1,6,0,5] to find maximum value 6 - this becomes our root
2
Divide Departments
Split into left team [3,2,1] and right team [0,5] based on position relative to 6
3
Recursive Organization
Apply same logic: 3 leads left team, 5 leads right team
4
Complete Hierarchy
Continue until every person has their position in the organization chart
Key Takeaway
๐ŸŽฏ Key Insight: The monotonic decreasing stack approach leverages the fact that in a maximum binary tree, each node's value is greater than all values in its subtrees, allowing us to build the tree in one efficient pass.
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
52.0K Views
Medium Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen