You are given an integer array nums
with no duplicates. A maximum binary tree can be built recursively from nums
using the following algorithm:
nums
.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 follows:
[3,2,1,6,0,5]
is 6. Left prefix is [3,2,1]
and right suffix is [0,5]
.
[3,2,1]
is 3. Left prefix is []
and right suffix is [2,1]
.
[2,1]
is 2. Left prefix is []
and right suffix is [1]
.
[0,5]
is 5. Left prefix is [0]
and right suffix is []
.
Example 2:
Input: nums = [3,2,1]
Output: [3,null,2,null,1]
Constraints:
1 <= nums.length <= 1000
0 <= nums[i] <= 1000
nums
are unique.How would you implement an algorithm to construct the maximum binary tree from the given integer array?
Given an integer array nums
with no duplicates, construct a maximum binary tree as follows:
nums
.Return the constructed maximum binary tree.
A straightforward approach is to recursively find the maximum element in the array, create a node for it, and then recursively call the function for the left and right subarrays. This approach involves repeatedly scanning subarrays to find the maximum element.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def constructMaximumBinaryTree(nums):
if not nums:
return None
max_val = max(nums)
max_index = nums.index(max_val)
root = TreeNode(max_val)
root.left = constructMaximumBinaryTree(nums[:max_index])
root.right = constructMaximumBinaryTree(nums[max_index+1:])
return root
The time complexity is O(n^2) in the worst case, where n is the number of elements in nums
. This is because, in each recursive call, we find the maximum element in O(n) time and make two recursive calls on subarrays.
The space complexity is O(n) due to the recursion stack. In the worst case (e.g., sorted array), the recursion depth can be n.
A more efficient approach involves using a stack to keep track of potentially larger elements. This method reduces the time complexity by avoiding repeated scans for the maximum element. The algorithm processes the array from left to right, maintaining a stack of nodes that could be part of the left subtree of the current node.
nums
array.class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def constructMaximumBinaryTreeOptimal(nums):
stack = []
for num in nums:
node = TreeNode(num)
while stack and stack[-1].val < num:
node.left = stack.pop()
if stack:
stack[-1].right = node
stack.append(node)
return stack[0] if stack else None
The time complexity is O(n), where n is the number of elements in nums
. Each element is pushed onto the stack once and popped at most once.
The space complexity is O(n) because, in the worst-case scenario (e.g., a sorted array), all elements could be stored in the stack.
None
.The optimal approach using a stack provides a more efficient way to construct the maximum binary tree with a time complexity of O(n) compared to the naive approach's O(n^2) time complexity. Both approaches have a space complexity of O(n).