0
0
DSA Javascriptprogramming~20 mins

Zigzag Level Order Traversal in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Zigzag Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
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));
A[[1],[3,2],[4,5]]
B[[1],[2,3],[5,4]]
C[[1],[3,2],[5,4]]
D[[1],[2,3],[4,5]]
Attempts:
2 left
💡 Hint
Remember that the traversal direction alternates at each level.
🧠 Conceptual
intermediate
1:30remaining
Understanding the zigzag traversal direction toggle
In zigzag level order traversal, why do we toggle the direction of traversal after each level?
ATo prioritize visiting leaf nodes before internal nodes.
BTo visit all nodes in the tree in ascending order of their values.
CTo ensure nodes are visited in a spiral pattern alternating left-to-right and right-to-left at each level.
DTo reduce the time complexity of the traversal.
Attempts:
2 left
💡 Hint
Think about the pattern the traversal creates visually.
🔧 Debug
advanced
2: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;
}
ASyntaxError: Unexpected token
BTypeError: Cannot read property 'val' of undefined
CInfinite loop causing program to hang
DNo error, outputs correct zigzag traversal
Attempts:
2 left
💡 Hint
Check the loop boundary condition in the for loop.
Predict Output
advanced
2: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
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));
A[[10],[15,6],[3,8,20],[25,17,7]]
B[[10],[15,6],[3,8,20],[7,17,25]]
C[[10],[6,15],[20,8,3],[7,17,25]]
D[[10],[6,15],[3,8,20],[7,17,25]]
Attempts:
2 left
💡 Hint
Remember the direction alternates each level starting left to right.
🧠 Conceptual
expert
1: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?
ABecause push would cause nodes to be visited twice.
BBecause unshift is faster than push for arrays in JavaScript.
CBecause unshift removes duplicates automatically.
DBecause unshift inserts elements at the beginning, reversing the order of nodes at that level.
Attempts:
2 left
💡 Hint
Think about how the order of nodes at a level is reversed.