LeetCode Problem

How to Solve Maximum Binary Tree

To solve Maximum Binary Tree, identify the largest element in the array as the root, recursively build the left subtree from elements before it and the right subtree from elements after it. Careful state tracking prevents incorrect subtree assignments. Using recursion or a monotonic stack ensures linear or divide-and-conquer efficiency depending on implementation.

GhostInterview Help

Need help with Maximum Binary Tree without spending extra time grinding it?

GhostInterview can read Maximum Binary Tree from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #654Binary-tree traversal and state trackingReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Binary-tree traversal and state tracking
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve Maximum Binary Tree, identify the largest element in the array as the root, recursively build the left subtree from elements before it and the right subtree from elements after it. Careful state tracking prevents incorrect subtree assignments. Using recursion or a monotonic stack ensures linear or divide-and-conquer efficiency depending on implementation.

Problem Statement

Given an integer array nums with unique elements, construct a maximum binary tree using this rule: the root is the largest element in the array, the left subtree is the maximum tree built from the subarray left of the root, and the right subtree is built from the subarray right of the root. Return the constructed tree.

For example, given nums = [3,2,1,6,0,5], the root is 6, the left subtree is built from [3,2,1], and the right subtree from [0,5]. Each subtree follows the same recursive rule, producing a tree that reflects the maximum values hierarchy.

Examples

Example 1

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

Output: [6,3,5,null,2,0,null,null,1]

The recursive calls are as follow:

  • The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
  • The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
  • Empty array, so no child.
  • The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
  • Empty array, so no child.
  • Only one element, so child is a node with value 1.
  • The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
  • Only one element, so child is a node with value 0.
  • Empty array, so no child.

Example 2

Input: nums = [3,2,1]

Output: [3,null,2,null,1]

Example details omitted.

Constraints

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

Solution Approach

Recursive Divide-and-Conquer

Find the maximum in the current array segment, make it the root node, and recursively construct left and right subtrees from the subarrays. Maintain array bounds to prevent index errors.

Monotonic Stack Optimization

Iterate through the array using a stack to maintain decreasing elements. When a new element is larger than the stack top, pop nodes and assign them as left children. This reduces repeated scans for the maximum, improving efficiency.

Tree Traversal Validation

After construction, verify the tree with preorder or inorder traversal to ensure each node respects the maximum binary tree property. This helps catch misassigned left or right children caused by index or recursion mistakes.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(n^2) for simple recursion due to repeated max searches, but O(n) with a monotonic stack. Space complexity is O(n) for recursion stack or explicit data structures.

What Interviewers Usually Probe

  • Expect you to explain recursive subtree construction and element selection.
  • They may probe for iterative alternatives using a stack to reduce repeated max searches.
  • Clarification questions often focus on handling empty arrays or single-element subarrays.

Common Pitfalls or Variants

Common pitfalls

  • Not correctly partitioning the array, leading to misplaced children.
  • Repeatedly scanning for the maximum without optimization, causing O(n^2) time.
  • Ignoring edge cases like empty subarrays or single-element segments.

Follow-up variants

  • Construct a maximum binary tree from a linked list instead of an array.
  • Build a minimum binary tree by selecting the smallest element as root at each step.
  • Return the preorder or inorder traversal of the maximum binary tree without building it explicitly.

How GhostInterview Helps

  • Automatically constructs the maximum binary tree for given arrays and checks subtree correctness.
  • Highlights recursive and stack-based approaches, showing the exact state transitions.
  • Validates edge cases and array partitions to prevent common index and child-assignment errors.

Topic Pages

FAQ

What is the main pattern used in Maximum Binary Tree?

The problem follows a binary-tree traversal and state tracking pattern, recursively dividing the array based on the maximum element.

Can Maximum Binary Tree be built iteratively?

Yes, using a monotonic stack approach, which assigns left children when popping smaller elements, reducing redundant maximum searches.

What is the time complexity for simple recursive construction?

It is O(n^2) because each recursive call scans the subarray for the maximum element.

How do you handle empty subarrays in recursion?

Return null for empty subarrays to ensure the parent node has no child in that direction.

Are all elements in nums unique for this problem?

Yes, Maximum Binary Tree requires that all integers in nums are unique to define a clear maximum at each step.

GhostInterview Solver

Need direct help with Maximum Binary Tree instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Maximum Binary Tree from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.