Leetcode 654: Maximum Binary Tree
Given an integer array nums with no duplicates, construct a maximum binary tree by recursively selecting the largest number as the root, and building the left and right subtrees from the elements before and after the largest number.
Problem
Approach
Steps
Complexity
Input: The input is an integer array with unique values.
Example: nums = [6, 3, 8, 2, 5, 7, 4]
Constraints:
• 1 <= nums.length <= 1000
• 0 <= nums[i] <= 1000
• All integers in nums are unique
Output: Return the root of the maximum binary tree, where each subtree is constructed according to the specified rules.
Example: [8,3,7,2,5,null,4]
Constraints:
• The output will be a binary tree structure representing the maximum binary tree.
Goal: Build a binary tree where each node is the largest value in a subarray.
Steps:
• 1. Find the largest element in the array.
• 2. Make that element the root of the tree.
• 3. Recursively build the left subtree using the elements before the largest element.
• 4. Recursively build the right subtree using the elements after the largest element.
Goal: The array contains unique integers, and the number of elements is between 1 and 1000.
Steps:
• 1 <= nums.length <= 1000
• 0 <= nums[i] <= 1000
• All integers in nums are unique
Assumptions:
• The input array will always contain at least one element.
• Input: nums = [6, 3, 8, 2, 5, 7, 4]
• Explanation: The largest number, 8, becomes the root. The subarrays to the left and right are [6, 3] and [2, 5, 7, 4], respectively. These subarrays are processed recursively, with 6 as the left child and 7 as the right child of the root.
Approach: To construct the maximum binary tree, we find the largest value in the current subarray, make it the root, and then recursively build the left and right subtrees from the remaining elements.
Observations:
• The maximum binary tree can be built by using a divide-and-conquer approach.
• This problem requires finding the maximum value efficiently in the array and handling the recursion correctly for left and right subtrees.
Steps:
• 1. Iterate through the array to find the maximum value.
• 2. Create a new node with the maximum value.
• 3. Recursively build the left subtree from the left subarray and the right subtree from the right subarray.
• 4. Repeat the process for each subtree.
Empty Inputs:
• The input array will not be empty as it contains at least one element.
Large Inputs:
• For large inputs, ensure that the solution handles up to 1000 elements efficiently.
Special Values:
• Handle cases where the array is in strictly increasing or strictly decreasing order.
Constraints:
• Ensure that the solution is optimized for time complexity, especially for larger arrays.
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
vector<TreeNode*> stk;
for(int num : nums) {
TreeNode* cur = new TreeNode(num);
while(!stk.empty() && stk.back()->val < num) {
cur->left = stk.back();
stk.pop_back();
}
if(!stk.empty()) stk.back()->right = cur;
stk.push_back(cur);
}
return stk.front();
}
1 : Function Definition
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
This is the function header for `constructMaximumBinaryTree`. It takes a vector of integers `nums` and returns a pointer to the root of the constructed binary tree.
2 : Stack Initialization
vector<TreeNode*> stk;
A stack `stk` is initialized to help in constructing the binary tree. The stack will hold nodes as we build the tree.
3 : Loop Through Input
for(int num : nums) {
The function iterates over each integer in the `nums` vector to construct the binary tree.
4 : Node Creation
TreeNode* cur = new TreeNode(num);
A new `TreeNode` is created for each value in the `nums` vector. This node will eventually be part of the binary tree.
5 : While Loop for Left Children
while(!stk.empty() && stk.back()->val < num) {
The `while` loop checks if the last node in the stack has a smaller value than the current number. If true, this means the current node will be a parent of the nodes in the stack.
6 : Link Left Child
cur->left = stk.back();
The node at the top of the stack becomes the left child of the current node, as the current node has a larger value.
7 : Pop Stack
stk.pop_back();
The node is removed from the stack after being linked as the left child of the current node.
8 : Link Right Child
if(!stk.empty()) stk.back()->right = cur;
If the stack is not empty, the current node is set as the right child of the node at the top of the stack.
9 : Push Current Node to Stack
stk.push_back(cur);
The current node is added to the stack to be used as a potential parent for the next node.
10 : Return Root
return stk.front();
The root of the tree, which is at the front of the stack after the loop completes, is returned.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is linear, O(n), because each element is processed once.
Best Case: O(n)
Worst Case: O(n)
Description: The space complexity is O(n) due to the recursion stack and the tree nodes.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus