Maximum Binary Tree
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. Return the result as an array.
Examples
nums = [3,2,1,6,0,5][6,3,5,null,2,0,null,null,1]Max tree built.
nums = [1,2,3,4,5][5,4,null,3,null,2,null,1]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.
nums = [7,4,8,2,6][8,7,6,null,4,2]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
Ready to solve this problem?
Practice solo and sharpen your skills for technical interviews.