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
.
For example, given nums = [3,2,1,6,0,5]
, the expected output is a binary tree represented as [6,3,5,null,2,0,null,null,1]
. The largest value is 6, making it the root. The left subtree is built from [3,2,1]
, and the right subtree from [0,5]
. Similarly, if nums = [3,2,1]
, the tree would be [3,null,2,null,1]
.
Write a function to construct the maximum binary tree from the given array. Explain the time and space complexity of your solution. Also, describe any edge cases that need to be considered. Provide example code.
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
.
A brute-force approach would involve, for each subarray, finding the maximum element and recursively building the tree. This involves repeatedly scanning subarrays to find the maximum.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def constructMaximumBinaryTree_naive(nums):
if not nums:
return None
max_val = max(nums)
max_index = nums.index(max_val)
root = TreeNode(max_val)
root.left = constructMaximumBinaryTree_naive(nums[:max_index])
root.right = constructMaximumBinaryTree_naive(nums[max_index+1:])
return root
O(n^2) in the worst case, where n is the number of elements in nums
. Finding the maximum element and its index in each subarray takes O(n) time, and this operation is performed for each node in the tree.
O(n) due to the recursive call stack. In the worst case (skewed tree), the depth of the recursion can be n.
A more efficient approach involves using a stack to keep track of potential parent nodes. This avoids repeatedly scanning subarrays to find the maximum element.
nums
array.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def constructMaximumBinaryTree(nums):
stack = []
for num in nums:
node = TreeNode(num)
while stack and num > stack[-1].val:
node.left = stack.pop()
if stack:
stack[-1].right = node
stack.append(node)
return stack[0]
O(n), where n is the number of elements in nums
. Each element is pushed onto and popped from the stack at most once.
O(n) in the worst case, where n is the number of elements in nums
. The stack can contain all the elements if the input array is sorted in ascending order.
None
.