Challenge - 5 Problems
Level Order Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this level order traversal?
Given the binary tree below, what is the printed output of a level order traversal (BFS)?
Tree structure:
10
/ \
5 15
/ \ \
3 7 18
Tree structure:
10
/ \
5 15
/ \ \
3 7 18
DSA C
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void levelOrder(struct Node* root) {
if (!root) return;
struct Node* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear) {
struct Node* current = queue[front++];
printf("%d ", current->data);
if (current->left) queue[rear++] = current->left;
if (current->right) queue[rear++] = current->right;
}
}Attempts:
2 left
💡 Hint
Level order traversal visits nodes level by level from left to right.
✗ Incorrect
Level order traversal uses a queue to visit nodes starting from the root, then its children from left to right, then their children, and so on. So the output is 10 (root), then 5 and 15 (level 2), then 3, 7, and 18 (level 3).
🧠 Conceptual
intermediateWhat data structure is essential for level order traversal?
Which data structure is primarily used to implement level order traversal (BFS) on a binary tree?
Attempts:
2 left
💡 Hint
Think about the order nodes are visited in BFS.
✗ Incorrect
A queue is used because it follows First-In-First-Out (FIFO) order, which matches the level order traversal visiting nodes level by level.
🔧 Debug
advancedWhy does this level order traversal code cause an infinite loop?
Consider this code snippet for level order traversal. Why does it cause an infinite loop?
Code snippet:
int front = 0, rear = 0;
queue[rear++] = root;
while (front <= rear) {
Node* current = queue[front++];
printf("%d ", current->data);
if (current->left) queue[rear++] = current->left;
if (current->right) queue[rear++] = current->right;
}
Code snippet:
int front = 0, rear = 0;
queue[rear++] = root;
while (front <= rear) {
Node* current = queue[front++];
printf("%d ", current->data);
if (current->left) queue[rear++] = current->left;
if (current->right) queue[rear++] = current->right;
}
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
Using 'front <= rear' causes the loop to run one extra time when front equals rear, leading to accessing an invalid queue element and possibly infinite loop. The correct condition is 'front < rear'.
❓ Predict Output
advancedWhat is the output of this modified level order traversal?
This code prints nodes level by level but prints a newline after each level. What is the output?
Tree:
1
/ \
2 3
/ / \
4 5 6
Code snippet:
void levelOrderNewline(Node* root) {
if (!root) return;
Node* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear) {
int count = rear - front;
for (int i = 0; i < count; i++) {
Node* current = queue[front++];
printf("%d ", current->data);
if (current->left) queue[rear++] = current->left;
if (current->right) queue[rear++] = current->right;
}
printf("\n");
}
}
Tree:
1
/ \
2 3
/ / \
4 5 6
Code snippet:
void levelOrderNewline(Node* root) {
if (!root) return;
Node* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear) {
int count = rear - front;
for (int i = 0; i < count; i++) {
Node* current = queue[front++];
printf("%d ", current->data);
if (current->left) queue[rear++] = current->left;
if (current->right) queue[rear++] = current->right;
}
printf("\n");
}
}
Attempts:
2 left
💡 Hint
Each level is printed on its own line.
✗ Incorrect
The code prints nodes level by level. First level: 1, second level: 2 3, third level: 4 5 6, each followed by a newline.
🧠 Conceptual
expertWhat is the time complexity of level order traversal on a binary tree with n nodes?
Consider a binary tree with n nodes. What is the time complexity of performing a level order traversal (BFS) on this tree?
Attempts:
2 left
💡 Hint
Each node is visited exactly once.
✗ Incorrect
Level order traversal visits each node once and processes its children, so the time complexity is linear in the number of nodes, O(n).
