Challenge - 5 Problems
Zigzag Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Zigzag Traversal on a Balanced Tree
What is the output of the zigzag level order traversal for the following binary tree?
Tree structure:
1
/ \
2 3
/ \ / \
4 5 6 7
Tree structure:
1
/ \
2 3
/ \ / \
4 5 6 7
DSA C
/* Zigzag traversal visits nodes level by level. Odd levels left to right, even levels right to left. */ // Expected output format: [[1], [3, 2], [4, 5, 6, 7]]
Attempts:
2 left
💡 Hint
Remember zigzag means alternate direction each level.
✗ Incorrect
Level 1: left to right → [1]
Level 2: right to left → [3, 2]
Level 3: left to right → [4, 5, 6, 7]
Level 2: right to left → [3, 2]
Level 3: left to right → [4, 5, 6, 7]
🧠 Conceptual
intermediateUnderstanding Zigzag Traversal Direction
In zigzag level order traversal, what determines the direction of traversal at each level?
Attempts:
2 left
💡 Hint
Think about how the traversal alternates direction.
✗ Incorrect
Zigzag traversal alternates direction based on whether the level number is odd or even.
🔧 Debug
advancedIdentify the Error in Zigzag Traversal Code
What error will this C code snippet cause when performing zigzag level order traversal?
```c
if (level % 2 == 0) {
// traverse left to right
for (int i = 0; i < size; i++) {
printf("%d ", queue[i]->val);
}
} else {
// traverse right to left
for (int i = size - 1; i >= 0; i--) {
printf("%d ", queue[i]->val);
}
}
```
Assume level starts at 1 for root level.
DSA C
/* level starts at 1 */Attempts:
2 left
💡 Hint
Check how parity relates to direction and level numbering.
✗ Incorrect
If level starts at 1, even levels should be right to left, but code does left to right on even levels, reversing directions.
🚀 Application
advancedZigzag Traversal Output for Unbalanced Tree
Given this unbalanced binary tree:
10
/ \
5 15
\ \
7 20
/
17
What is the zigzag level order traversal output?
10
/ \
5 15
\ \
7 20
/
17
What is the zigzag level order traversal output?
Attempts:
2 left
💡 Hint
Remember to alternate direction each level.
✗ Incorrect
Level 1: left to right → [10]
Level 2: right to left → [15, 5]
Level 3: left to right → [7, 20]
Level 4: right to left → [17]
Level 2: right to left → [15, 5]
Level 3: left to right → [7, 20]
Level 4: right to left → [17]
❓ Predict Output
expertOutput of Zigzag Traversal with Null Nodes Included
Consider this C code snippet that attempts zigzag traversal but includes NULL nodes in the queue:
```c
// queue contains nodes including NULLs
for (int i = 0; i < size; i++) {
if (queue[i] != NULL) {
printf("%d ", queue[i]->val);
} else {
printf("null ");
}
}
```
If the queue at a level is [node(1), NULL, node(3)], what is the printed output?
Attempts:
2 left
💡 Hint
Check how NULL nodes are handled in the print loop.
✗ Incorrect
The code prints 'null ' for NULL nodes and the value for non-NULL nodes, so output is '1 null 3 '.
