0
0
DSA Javascriptprogramming~10 mins

Zigzag Level Order Traversal in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Alevels
Bqueue
Cstack
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'queue' instead of 'result' for storing output.
Not initializing an array before use.
2fill in blank
medium

Complete 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'
Apop
Bshift
Cpush
Dunshift
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.
3fill in blank
hard

Fix 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'
Aunshift
Bpush
Cshift
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' instead of 'unshift' causing wrong order.
Not toggling the direction boolean properly.
4fill in blank
hard

Fill 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'
Aleft
Bright
Cval
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'parent' instead of 'left' or 'right'.
Not checking if child exists before pushing.
5fill in blank
hard

Fill 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'
Ashift
Bunshift
Cleft
Dpop
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.