Leetcode 102: Binary Tree Level Order Traversal
Given the root of a binary tree, your task is to return the level order traversal of its nodes’ values. This means you should traverse the tree level by level, from left to right at each level.
Problem
Approach
Steps
Complexity
Input: You are given the root of a binary tree, where each node has a value and pointers to its left and right children.
Example: root = [5,3,8,1,4,null,9]
Constraints:
• The number of nodes in the tree is in the range [0, 2000].
• -1000 <= Node.val <= 1000
Output: The output should be a 2D array where each element represents a level in the binary tree. Each level is a list of node values at that level, traversed from left to right.
Example: Output: [[5], [3, 8], [1, 4, 9]]
Constraints:
• The output should be an array of arrays where each inner array contains the values of the nodes at that level.
Goal: The goal is to perform a level order traversal of the binary tree using a queue to process nodes level by level.
Steps:
• Start by checking if the root is null. If it is, return an empty list.
• Initialize a queue with the root node.
• While the queue is not empty, process each node at the current level.
• For each level, create a list of node values and add the left and right children of each node to the queue.
Goal: Ensure the solution can handle a tree with up to 2000 nodes efficiently.
Steps:
• The tree can have between 0 and 2000 nodes.
Assumptions:
• The input is always a valid binary tree.
• Input: root = [3,9,20,null,null,15,7]
• Explanation: The binary tree has the following structure:
3
/ \
9 20
/ \
15 7
The level order traversal is: [[3], [9, 20], [15, 7]]
• Input: root = [1]
• Explanation: The binary tree consists of just one node, which is the root node. The level order traversal is: [[1]]
• Input: root = []
• Explanation: An empty tree has no nodes, so the level order traversal is: []
Approach: The problem can be solved using a breadth-first search (BFS) approach where we explore the tree level by level, ensuring nodes at each level are processed from left to right.
Observations:
• Level order traversal can be efficiently implemented using a queue.
• By using a queue, we can ensure that we process nodes level by level, which is the key to solving this problem.
Steps:
• Check if the root is null, return an empty list if it is.
• Initialize a queue and add the root node to it.
• While the queue is not empty, process each level by iterating over the nodes in the queue.
• For each node in the current level, add its value to the result list and enqueue its left and right children if they exist.
Empty Inputs:
• If the tree is empty (i.e., the root is null), return an empty list.
Large Inputs:
• For large trees with up to 2000 nodes, ensure the solution handles the input efficiently.
Special Values:
• If a node has only one child (either left or right), make sure it is properly handled during the level order traversal.
Constraints:
• The tree must be processed within the provided constraints (2000 nodes).
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ans;
if(!root) return ans;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
int sz = q.size();
vector<int> res;
while(sz--) {
TreeNode* tmp = q.front();
res.push_back(tmp->val);
q.pop();
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
ans.push_back(res);
}
return ans;
}
1 : Function Definition
vector<vector<int>> levelOrder(TreeNode* root) {
Define the function 'levelOrder' which takes the root of the tree and returns a 2D vector of integers, representing the tree's level-order traversal.
2 : Initialization
vector<vector<int>> ans;
Initialize an empty 2D vector 'ans' to store the values of each level of the tree.
3 : Base Case
if(!root) return ans;
If the tree is empty (root is NULL), return the empty result vector.
4 : Queue Initialization
queue<TreeNode*> q;
Create a queue to assist in performing a breadth-first search (BFS). The queue will hold nodes at each level.
5 : Push Root to Queue
q.push(root);
Push the root node to the queue to begin the level-order traversal.
6 : Main BFS Loop
while(!q.empty()) {
While the queue is not empty, continue processing the nodes level by level.
7 : Level Size
int sz = q.size();
Store the current size of the queue, which represents the number of nodes at the current level.
8 : Result Vector
vector<int> res;
Initialize a temporary vector 'res' to hold the node values for the current level.
9 : Level Nodes Processing
while(sz--) {
Process each node at the current level by looping through the queue until all nodes at this level are visited.
10 : Queue Front
TreeNode* tmp = q.front();
Get the front node from the queue, which is the current node to be processed.
11 : Add Node Value
res.push_back(tmp->val);
Add the value of the current node to the 'res' vector.
12 : Queue Pop
q.pop();
Pop the processed node from the queue.
13 : Left Child Check
if(tmp->left) q.push(tmp->left);
If the current node has a left child, push it onto the queue for the next level.
14 : Right Child Check
if(tmp->right) q.push(tmp->right);
If the current node has a right child, push it onto the queue for the next level.
15 : Push Level to Result
ans.push_back(res);
After processing all nodes at the current level, add the 'res' vector to the final result vector 'ans'.
16 : Return Result
return ans;
Return the final 2D vector 'ans' which contains the level-order traversal of the tree.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: In all cases, we must visit each node in the tree once, so the time complexity is O(n), where n is the number of nodes in the tree.
Best Case: O(1)
Worst Case: O(n)
Description: In the worst case, the queue may hold all the nodes at the last level of the tree, resulting in a space complexity of O(n). In the best case, when the tree is perfectly balanced, the space complexity could be reduced to O(log n).
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus