Challenge - 5 Problems
Zigzag Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Zigzag Level Order Traversal on a simple tree
What is the output of the zigzag level order traversal for the following binary tree?
Tree structure:
1
/ \
2 3
/ \
4 5
Tree structure:
1
/ \
2 3
/ \
4 5
DSA Javascript
function zigzagLevelOrder(root) {
if (!root) return [];
const result = [];
const queue = [root];
let leftToRight = true;
while (queue.length) {
const levelSize = queue.length;
const levelNodes = [];
for (let i = 0; i < levelSize; i++) {
const node = queue.shift();
if (leftToRight) {
levelNodes.push(node.val);
} else {
levelNodes.unshift(node.val);
}
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
result.push(levelNodes);
leftToRight = !leftToRight;
}
return result;
}
// Tree nodes
const root = { val: 1, left: { val: 2, left: { val: 4, left: null, right: null }, right: null }, right: { val: 3, left: null, right: { val: 5, left: null, right: null } } };
console.log(zigzagLevelOrder(root));Attempts:
2 left
💡 Hint
Remember that the traversal direction alternates at each level.
✗ Incorrect
The traversal starts left to right at level 0: [1]. Then right to left at level 1: [3, 2]. Then left to right at level 2: [4, 5].
🧠 Conceptual
intermediate1:30remaining
Understanding the zigzag traversal direction toggle
In zigzag level order traversal, why do we toggle the direction of traversal after each level?
Attempts:
2 left
💡 Hint
Think about the pattern the traversal creates visually.
✗ Incorrect
Toggling direction creates a zigzag or spiral pattern, alternating the order nodes are visited at each level.
🔧 Debug
advanced2:00remaining
Identify the error in this zigzag traversal implementation
What error does the following code produce when run on a non-empty binary tree?
DSA Javascript
function zigzagLevelOrder(root) {
if (!root) return [];
const result = [];
const queue = [root];
let leftToRight = true;
while (queue.length) {
const levelSize = queue.length;
const levelNodes = [];
for (let i = 0; i <= levelSize; i++) {
const node = queue.shift();
if (leftToRight) {
levelNodes.push(node.val);
} else {
levelNodes.unshift(node.val);
}
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
result.push(levelNodes);
leftToRight = !leftToRight;
}
return result;
}Attempts:
2 left
💡 Hint
Check the loop boundary condition in the for loop.
✗ Incorrect
The for loop uses i <= levelSize, which causes one extra iteration where queue.shift() returns undefined, leading to accessing 'val' of undefined.
❓ Predict Output
advanced2:30remaining
Output of zigzag traversal on a larger tree
What is the output of the zigzag level order traversal for this tree?
Tree structure:
10
/ \
6 15
/ \ \
3 8 20
/ / \
7 17 25
Tree structure:
10
/ \
6 15
/ \ \
3 8 20
/ / \
7 17 25
DSA Javascript
function zigzagLevelOrder(root) {
if (!root) return [];
const result = [];
const queue = [root];
let leftToRight = true;
while (queue.length) {
const levelSize = queue.length;
const levelNodes = [];
for (let i = 0; i < levelSize; i++) {
const node = queue.shift();
if (leftToRight) {
levelNodes.push(node.val);
} else {
levelNodes.unshift(node.val);
}
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
result.push(levelNodes);
leftToRight = !leftToRight;
}
return result;
}
const root = {
val: 10,
left: {
val: 6,
left: { val: 3, left: null, right: null },
right: { val: 8, left: { val: 7, left: null, right: null }, right: null }
},
right: {
val: 15,
left: null,
right: {
val: 20,
left: { val: 17, left: null, right: null },
right: { val: 25, left: null, right: null }
}
}
};
console.log(zigzagLevelOrder(root));Attempts:
2 left
💡 Hint
Remember the direction alternates each level starting left to right.
✗ Incorrect
Level 0: left to right [10]
Level 1: right to left [15,6]
Level 2: left to right [3,8,20]
Level 3: right to left [25,17,7]
Level 1: right to left [15,6]
Level 2: left to right [3,8,20]
Level 3: right to left [25,17,7]
🧠 Conceptual
expert1:30remaining
Why use unshift for right-to-left insertion in zigzag traversal?
In the zigzag level order traversal, why do we use unshift to add node values when traversing right to left instead of push?
Attempts:
2 left
💡 Hint
Think about how the order of nodes at a level is reversed.
✗ Incorrect
Using unshift inserts each node value at the front of the array, reversing the order of nodes collected at that level to achieve right-to-left order.