AlgoMaster Logo

Maximum Binary Tree

Last Updated: April 1, 2026

medium
4 min read

Understanding the Problem

We need to construct a binary tree from an array using a specific rule: the root of any subtree is always the maximum element in the current subarray, and the elements to its left and right form the left and right subtrees respectively.

This is essentially a divide and conquer problem. At each level of recursion, we pick the maximum element as the root, then split the array around it. The structure of the resulting tree depends entirely on where the maximums fall relative to each other.

The key observation is that this construction is closely related to a data structure called a Cartesian tree, where the tree maintains both heap property (parent is larger than children) and BST property on array indices (left children come from indices before the root, right children from indices after).

Key Constraints:

  • 1 <= nums.length <= 1000 -- With n up to 1000, even an O(n^2) brute force is perfectly fine. We have at most ~1 million operations. But an O(n) approach exists using a monotonic stack.
  • 0 <= nums[i] <= 1000 -- All values are non-negative. No special handling for negatives needed.
  • All integers are unique -- No ties to break when finding the maximum. This simplifies the logic and guarantees a unique tree structure.

Approach 1: Recursive Divide and Conquer

Intuition

The problem definition practically hands us the algorithm. We need to find the maximum, make it the root, and recurse on both sides. So the most natural approach is a direct recursive implementation of the construction rules.

At each step, we scan the current subarray to find the index of the maximum value. That maximum becomes the root node. Everything to its left in the array forms the left subtree, and everything to its right forms the right subtree. We recurse until the subarray is empty.

The reason this works is that the definition itself is recursive. The maximum binary tree of any subarray is: the max as root, with the maximum binary tree of the left portion as the left child and the maximum binary tree of the right portion as the right child. It is a textbook divide and conquer pattern.

Algorithm

  1. If the current subarray range is empty (left > right), return null.
  2. Scan the subarray from left to right to find the index of the maximum element.
  3. Create a new tree node with the maximum value.
  4. Recursively build the left subtree using elements from left to maxIndex - 1.
  5. Recursively build the right subtree using elements from maxIndex + 1 to right.
  6. Return the root node.

Example Walkthrough

1build(0, 5): Scan full array for max → 6 at index 3
0
3
1
2
2
1
3
6
max=6
4
0
5
5
range
1/7

Code

The recursive approach mirrors the problem definition directly, but it rescans the subarray for the maximum at every level of recursion. What if we could build the entire tree in a single left-to-right pass?

Approach 2: Monotonic Stack (Optimal)

Intuition

Instead of repeatedly scanning for maximums, we can process the array from left to right and use a stack to maintain the "right spine" of the tree built so far. The stack holds nodes in decreasing order of value.

When a new value arrives that is smaller than the top, it becomes the right child of the top. When a new value is larger, we pop smaller nodes off the stack. The last popped node becomes the left child of our new node (because those smaller elements sit to the left of it in the array). If the stack is not empty after popping, our new node becomes the right child of the current top.

Each element is pushed exactly once and popped at most once, giving us O(n) total time.

Algorithm

  1. Create an empty stack that will hold tree nodes.
  2. Iterate through each element in nums from left to right.
  3. Create a new tree node for the current element.
  4. While the stack is not empty and the top of the stack has a smaller value than the current element, pop from the stack. The last popped node becomes the left child of the current node.
  5. If the stack is not empty, the current node becomes the right child of the top of the stack.
  6. Push the current node onto the stack.
  7. After processing all elements, the bottom of the stack is the root of the tree.

Example Walkthrough

1Start: process elements left to right
0
3
i
1
2
2
1
3
6
4
0
5
5
1/8
1Stack empty, ready to process
1/8

Code