AlgoMaster Logo

Maximum Binary Tree

mediumFrequency6 min readUpdated June 23, 2026

Understanding the Problem

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

This is 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 on where the maximums fall relative to each other.

This construction produces a Cartesian tree: a tree that satisfies the heap property on values (every parent is larger than its children) and the binary-search-tree property on the original array indices (left descendants come from indices before the root, right descendants from indices after). Those two properties together pin down a unique tree, which is why the array maps to exactly one maximum binary tree.

Key Constraints:

  • 1 <= nums.length <= 1000 -- With n up to 1000, an O(n^2) recursive solution runs in at most about a million operations, well within limits. An O(n) monotonic-stack solution also exists, which we cover as the second approach.
  • 0 <= nums[i] <= 1000 -- All values fit comfortably in a 32-bit integer, so there is no overflow concern in any language.
  • All integers are unique -- There are no ties to break when finding the maximum, so the tree structure is uniquely determined.

Approach 1: Recursive Divide and Conquer

Intuition

The definition is itself recursive, so it translates directly into code: find the maximum, make it the root, and recurse on the two halves around it.

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.

This is correct because the maximum binary tree of any subarray is defined as exactly this: 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. The code is a literal transcription of that definition.

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 rescans the subarray for the maximum at every level. The next approach removes that repeated work and builds the entire tree in a single left-to-right pass.

Approach 2: Monotonic Stack (Optimal)

Intuition

Instead of repeatedly scanning for maximums, we process the array from left to right and keep a stack holding the right spine of the tree built so far: the path from the current root down through successive right children. The values on the stack decrease from bottom to top.

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

Each element is pushed once and popped at most once, so the total time is O(n).

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

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

Code