Imagine you're an architect designing a skyline where each building's height represents a value in an array. Your task is to construct a Maximum Binary Tree using a special recursive algorithm that creates the most prominent structure possible.
Given an integer array nums with no duplicates, build a maximum binary tree following these rules:
- Find the Maximum: Create a root node with the maximum value in the current array
- Left Subtree: Recursively build the left subtree using all elements to the left of the maximum
- Right Subtree: Recursively build the right subtree using all elements to the right of the maximum
For example, with [3,2,1,6,0,5], the maximum is 6 at index 3. So 6 becomes the root, [3,2,1] forms the left subtree, and [0,5] forms the right subtree. This process continues recursively until every element finds its place in this hierarchical structure.
Goal: Return the root of the maximum binary tree built from nums.
Input & Output
Constraints
- 1 โค nums.length โค 1000
- 0 โค nums[i] โค 1000
- All integers in nums are unique