Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the result array for storing levels.
DSA Javascript
function zigzagLevelOrder(root) {
const [1] = [];
if (!root) return [];
// rest of code
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'queue' instead of 'result' for storing output.
Not initializing an array before use.
✗ Incorrect
We need an array named 'result' to store the nodes level by level in zigzag order.
2fill in blank
mediumComplete the code to add the root node to the queue for BFS traversal.
DSA Javascript
function zigzagLevelOrder(root) {
const result = [];
if (!root) return [];
const queue = [];
queue.[1](root);
// rest of code
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' or 'shift' which remove elements instead of adding.
Using 'unshift' which adds to the front, not the end.
✗ Incorrect
We add the root node to the end of the queue using 'push' to start BFS.
3fill in blank
hardFix the error in the code to toggle the direction for zigzag traversal.
DSA Javascript
let leftToRight = true; while (queue.length > 0) { 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.[1](node.val); } // add children } result.push(levelNodes); leftToRight = !leftToRight; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' instead of 'unshift' causing wrong order.
Not toggling the direction boolean properly.
✗ Incorrect
To add elements in reverse order, use 'unshift' to add at the front of the array.
4fill in blank
hardFill both blanks to add left and right children to the queue if they exist.
DSA Javascript
for (let i = 0; i < levelSize; i++) { const node = queue.shift(); if (node.[1]) queue.push(node.[1]); if (node.[2]) queue.push(node.[2]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'parent' instead of 'left' or 'right'.
Not checking if child exists before pushing.
✗ Incorrect
We add 'left' and 'right' children nodes to the queue if they exist.
5fill in blank
hardFill all three blanks to complete the zigzag level order traversal function.
DSA Javascript
function zigzagLevelOrder(root) {
if (!root) return [];
const result = [];
const queue = [root];
let leftToRight = true;
while (queue.length > 0) {
const levelSize = queue.length;
const levelNodes = [];
for (let i = 0; i < levelSize; i++) {
const node = queue.[1]();
if (leftToRight) {
levelNodes.push(node.val);
} else {
levelNodes.[2](node.val);
}
if (node.left) queue.push(node.[3]);
if (node.right) queue.push(node.right);
}
result.push(levelNodes);
leftToRight = !leftToRight;
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' instead of 'shift' breaks BFS order.
Using 'push' instead of 'unshift' for reverse order.
Using wrong child property name.
✗ Incorrect
We remove nodes from the front with 'shift', add reversed order with 'unshift', and add left child with 'left'.