Description
Given an integer array with no duplicates, build a Maximum Binary Tree recursively. The root is the maximum, left subtree from left of max, right from right.
Examples
Input:
nums = [3,2,1,6,0,5]Output:
[6,3,5,null,2,0,null,null,1]Explanation:
Max tree built.
Input:
nums = [1,2,3,4,5]Output:
[5,null,4,null,3,null,2,null,1]Explanation:
Since the array is in ascending order, each maximum element creates a right-skewed tree. 5 is the root, 4 becomes its right child (from subarray [4]), 3 becomes right child of 4 (from subarray [3]), and so on, creating a linear tree structure going right.
Input:
nums = [7,4,8,2,6]Output:
[8,7,null,4,null,null,null,null,6,2]Explanation:
The maximum element 8 at index 2 becomes the root. Left subtree is built from [7,4] with 7 as root and 4 as left child. Right subtree is built from [2,6] with 6 as root and 2 as left child. This creates a balanced structure with both left and right subtrees.
Constraints
- •
1 ≤ nums.length ≤ 1000