Leetcode 107: Binary Tree Level Order Traversal II
Given the root of a binary tree, return the bottom-up level order traversal of its nodes’ values. This means that for each level, starting from the leaf level and moving towards the root, you should collect the node values from left to right.
Problem
Approach
Steps
Complexity
Input: The input consists of a binary tree represented by its root node.
Example: root = [5,3,8,1,4,7,9]
Constraints:
• The number of nodes in the tree is in the range [0, 2000].
• -1000 <= Node.val <= 1000
Output: Return a list of lists representing the node values at each level from bottom to top.
Example: [[1, 4], [3, 8], [5]]
Constraints:
• The output should represent the nodes' values level by level from leaf to root.
Goal: The goal is to perform a bottom-up level order traversal of the binary tree, ensuring each level is collected in the correct order, from leaf to root.
Steps:
• Use a queue to perform a breadth-first traversal of the tree.
• For each level, collect the values into a list and store it.
• Since we need the levels from bottom to top, push each level's result onto a stack.
• Finally, pop the elements from the stack to return the bottom-up level order.
Goal: The solution should handle up to 2000 nodes in the tree efficiently.
Steps:
• The solution must handle the maximum number of nodes, 2000, efficiently.
Assumptions:
• The binary tree is non-empty, or it is an empty tree.
• Input: Input: root = [3,9,20,null,null,15,7]
• Explanation: In this case, the binary tree can be visualized as:
3
/ \
9 20
/ \
15 7
The bottom-up level order traversal is: [[15,7], [9,20], [3]].
• Input: Input: root = [1]
• Explanation: In this case, the binary tree consists of a single node 1. The bottom-up level order traversal is simply: [[1]].
• Input: Input: root = []
• Explanation: If the tree is empty, the bottom-up level order traversal should return an empty list: [].
Approach: The approach for solving this problem involves using a breadth-first search (BFS) to explore the tree level by level. Since we need the result in bottom-up order, we can utilize a stack to reverse the order after performing the traversal.
Observations:
• A level order traversal typically uses a queue, but we need the result in reverse order (bottom-up).
• We can store the level order results in a stack and then pop the stack to get the desired bottom-up order.
Steps:
• Initialize an empty queue and push the root of the tree into it.
• While the queue is not empty, process each level of the tree by iterating over the nodes at that level.
• For each node, push its children into the queue and store the node's value for that level.
• Push the list of values for each level into a stack.
• After processing all levels, pop the elements from the stack and return them as the result.
Empty Inputs:
• If the root is null (empty tree), return an empty list.
Large Inputs:
• The algorithm should be efficient enough to handle trees with up to 2000 nodes.
Special Values:
• Handle trees where all nodes are on one side (skewed trees).
Constraints:
• Ensure the solution works for both balanced and unbalanced trees.
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> ans;
queue<TreeNode*> q;
if(root == NULL) return ans;
q.push(root);
stack<vector<int>> stk;
while(!q.empty())
{
vector<int> ans;
int sz = q.size();
while(sz--) {
TreeNode* tmp = q.front();
q.pop();
ans.push_back(tmp->val);
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
stk.push(ans);
}
while(!stk.empty())
{
ans.push_back(stk.top());
stk.pop();
}
return ans;
}
1 : Function Definition
vector<vector<int>> levelOrderBottom(TreeNode* root) {
Define the function 'levelOrderBottom' to return the bottom-up level order traversal of the tree.
2 : Variable Declaration
vector<vector<int>> ans;
Declare a vector to store the final level-order traversal results.
3 : Variable Declaration
queue<TreeNode*> q;
Declare a queue to process tree nodes level by level.
4 : Base Case Check
if(root == NULL) return ans;
Check if the tree is empty. If yes, return an empty result.
5 : Queue Operation
q.push(root);
Push the root node into the queue to begin the level-order traversal.
6 : Variable Declaration
stack<vector<int>> stk;
Declare a stack to store the levels temporarily in reverse order.
7 : Loop Definition
while(!q.empty())
Start a loop to process each level of the tree until the queue is empty.
8 : Level Storage
vector<int> ans;
Declare a vector to store values of the current level.
9 : Queue Size
int sz = q.size();
Get the size of the current level by checking the number of nodes in the queue.
10 : Loop Definition
while(sz--) {
Loop through all nodes of the current level.
11 : Queue Operation
TreeNode* tmp = q.front();
Fetch the front node from the queue.
12 : Queue Operation
q.pop();
Remove the front node from the queue after processing.
13 : Node Value Collection
ans.push_back(tmp->val);
Add the value of the current node to the level vector.
14 : Conditional Function Call
if(tmp->left) q.push(tmp->left);
If the current node has a left child, add it to the queue for processing.
15 : Conditional Function Call
if(tmp->right) q.push(tmp->right);
If the current node has a right child, add it to the queue for processing.
16 : Stack Operation
stk.push(ans);
Push the current level vector onto the stack.
17 : Loop Definition
while(!stk.empty())
Start a loop to retrieve levels from the stack in reverse order.
18 : Stack Operation
ans.push_back(stk.top());
Add the top level vector from the stack to the result vector.
19 : Stack Operation
stk.pop();
Remove the top level from the stack after adding it to the result.
20 : Return Value
return ans;
Return the final bottom-up level order traversal vector.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is O(n), where n is the number of nodes in the tree, since each node is processed exactly once.
Best Case: O(n)
Worst Case: O(n)
Description: The space complexity is O(n) due to the space used by the queue and the stack to store the nodes and their values.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus