Skip to content

Practice Maximum Binary Tree II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#998
LeetCode Problem

Maximum Binary Tree II

Problem Statement

A maximum binary tree is built from an array of distinct values by choosing its maximum as root, recursively building the left subtree from the values before that maximum and the right subtree from the values after it; an empty subarray gives null. You receive this tree's root, not its original array. Append val to the end of that hidden array and return the maximum tree for the extended array. All values, including val, are distinct.

Source: Maximum Binary Tree II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
root = [4,1,3,null,null,2], val = 5
Output
[5,4,null,1,3,null,null,2]

# Example 2

Input
root = [5,2,4,null,1], val = 3
Output
[5,2,4,null,1,null,3]

# Example 3

Input
root = [5,2,3,null,1], val = 4
Output
[5,2,4,null,1,3]

Constraints

The number of nodes in the tree is in the range [1, 100].
1 <= Node.val <= 100
All the values of the tree are unique.
1 <= val <= 100

Related LeetCode Problems

Practice more problems similar to Maximum Binary Tree II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.