Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

654. Maximum Binary Tree

Medium85.7% Acceptance
ArrayDivide and ConquerStack
Asked by:
A
Amazon
ProblemSolutions (12)VideosCompanies (1)Notes

Problem Statement

You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:

  1. Create a root node whose value is the maximum value in nums.
  2. Recursively build the left subtree on the subarray prefix to the left of the maximum value.
  3. Recursively build the right subtree on the subarray suffix to the right of the maximum value.

Return the maximum binary tree built from nums.

Example 1:

Input: nums = [3,2,1,6,0,5]
Output: [6,3,5,null,2,0,null,null,1]
Explanation: 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]

Constraints:

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

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

The Maximum Binary Tree is built from an array where the root is the maximum element, the left subtree is built from elements to the left of the maximum, and the right subtree is built from elements to the right. A natural way to approach this is using divide and conquer. Find the maximum element in the current range, create a node, then recursively build the left and right subtrees. While simple and intuitive, repeatedly scanning for the maximum can increase the runtime in worst cases.

A more optimized strategy uses a monotonic decreasing stack. As you iterate through the array, maintain a stack where elements remain in decreasing order. When a larger element appears, pop smaller elements and attach them as the left child of the current node. The remaining stack top becomes the parent, connecting the current node as its right child. This technique constructs the tree in a single pass.

The stack-based approach achieves O(n) time complexity, while the recursive divide-and-conquer method can degrade to O(n²) in the worst case.

Complexity

ApproachTime ComplexitySpace Complexity
Divide and Conquer (Recursive)O(n^2) worst case, O(n log n) averageO(n) recursion stack
Monotonic StackO(n)O(n)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Recursive Division

Recursive Division Approach: This approach involves breaking down the problem using recursive calls. We start by identifying the maximum element in the array, which becomes the root of the tree. We then recursively split the array into sub-arrays on the left and right of the maximum element and construct the left and right subtrees respectively. This leverages the divide-and-conquer paradigm.

Time Complexity: O(n^2), where n is the number of elements, due to the repeated search for the maximum element. Space Complexity: O(n) for the recursion stack.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4struct TreeNode {
5    int val;
6    struct TreeNode 

Explanation

This C implementation uses a recursive function to build the maximum binary tree. The base case checks if the array is empty, returning NULL for the node. It finds the index of the maximum value, which becomes the root node's value, and then recursively builds the left and right subtrees from the elements to the left and right of this value.

Monotonic Stack

Monotonic Stack Approach: This approach involves utilizing a stack to directly simulate the necessary operations required to construct the maximum binary tree without recursion. By pushing and popping elements based on comparisons with the current element, we maintain a monotonic sequence from which the tree is constructed. This method leverages the stack to ensure that nodes are connected efficiently without the overhead of recursive calls.

Time Complexity: O(n), where n is the number of elements, ensured by single pass and stack operations. Space Complexity: O(n) due to stack storage.

CC++JavaPythonC#JavaScript
1

Video Solutions

Watch expert explanations and walkthroughs

Maximum Depth of Binary Tree - 3 Solutions - Leetcode 104 - Python

NeetCode
16:43296,166 views

Asked By Companies

1 companies
A
Amazon

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Median of Two Sorted ArraysHard
Maximum SubarrayMedium
Construct Binary Tree from Preorder and Inorder TraversalMedium
Construct Binary Tree from Inorder and Postorder TraversalMedium
More similar problems

Related Topics

ArrayDivide and ConquerStackTreeMonotonic StackBinary Tree

Problem Stats

Acceptance Rate85.7%
DifficultyMedium
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Maximum Binary Tree asked in FAANG interviews?

Yes, variations of Maximum Binary Tree appear in technical interviews at large tech companies. Interviewers use it to test understanding of stacks, tree construction, and divide-and-conquer strategies.

What is the optimal approach for Maximum Binary Tree?

The optimal approach uses a monotonic decreasing stack. By processing each element once and maintaining stack order, the binary tree can be constructed in linear time without repeatedly searching for maximum values.

Why is the monotonic stack useful in Maximum Binary Tree?

The monotonic stack ensures elements are processed in decreasing order, making it easy to identify parent-child relationships. This avoids repeated scans for maximum elements and reduces the overall time complexity to O(n).

What data structure is best for solving Maximum Binary Tree?

A monotonic stack is the most efficient data structure for this problem. It helps maintain decreasing order and allows quick attachment of left and right child relationships while building the tree.

*
left
;
7
struct
TreeNode
*
right
;
8
}
;
9
10
struct
TreeNode
*
constructMaximumBinaryTree
(
int
*
nums
,
int
numsSize
)
{
11
if
(
numsSize
==
0
)
return
NULL
;
12
int
maxIdx
=
0
;
13
for
(
int
i
=
1
;
i
<
numsSize
;
i
++
)
{
14
if
(
nums
[
i
]
>
nums
[
maxIdx
]
)
maxIdx
=
i
;
15
}
16
struct
TreeNode
*
root
=
(
struct
TreeNode
*
)
malloc
(
sizeof
(
struct
TreeNode
)
)
;
17
root
->
val
=
nums
[
maxIdx
]
;
18
root
->
left
=
constructMaximumBinaryTree
(
nums
,
maxIdx
)
;
19
root
->
right
=
constructMaximumBinaryTree
(
nums
+
maxIdx
+
1
,
numsSize
-
maxIdx
-
1
)
;
20
return
root
;
21
}
#
include
<stdio.h>
2
#
include
<stdlib.h>
3
4
struct
TreeNode
{
5
int
val
;
6
struct
TreeNode
*
left
;
7
struct
TreeNode
*
right
;
8
}
;
9
10
struct
TreeNode
*
constructMaximumBinaryTree
(
int
*
nums
,
int
numsSize
)
{
11
struct
TreeNode
*
*
stack
=
(
struct
TreeNode
*
*
)
malloc
(
sizeof
(
struct
TreeNode
*
)
*
numsSize
)
;
12
int
stackSize
=
0
;
13
for
(
int
i
=
0
;
i
<
numsSize
;
++
i
)
{
14
struct
TreeNode
*
current
=
(
struct
TreeNode
*
)
malloc
(
sizeof
(
struct
TreeNode
)
)
;
15
current
->
val
=
nums
[
i
]
;
16
current
->
left
=
NULL
;
17
current
->
right
=
NULL
;
18
while
(
stackSize
>
0
&&
stack
[
stackSize
-
1
]
->
val
<
nums
[
i
]
)
{
19
current
->
left
=
stack
[
stackSize
-
1
]
;
20
stackSize
--
;
21
}
22
if
(
stackSize
>
0
)
{
23
stack
[
stackSize
-
1
]
->
right
=
current
;
24
}
25
stack
[
stackSize
++
]
=
current
;
26
}
27
while
(
stackSize
>
1
)
{
28
stackSize
--
;
29
}
30
struct
TreeNode
*
root
=
stack
[
0
]
;
31
free
(
stack
)
;
32
return
root
;
33
}

Explanation

Description: This C solution uses a monotonic stack to keep track of nodes. As we iterate over the elements, we maintain the condition where each parent node's right child points to the current node, while popping off smaller elements and assigning them as left children.