Bird
Raised Fist0
DSA C#programming~10 mins

Zigzag Level Order Traversal in DSA C# - Interactive Practice

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

Complete the code to initialize the queue with the root node.

DSA C#
var result = new List<IList<int>>();
if (root == null) return result;
var queue = new Queue<TreeNode>();
queue.[1](root);
Drag options to blanks, or click blank then click option'
AEnqueue
BAdd
CPush
DInsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using Add() which is for List, not Queue.
Using Push() which is for Stack.
2fill in blank
medium

Complete the code to get the number of nodes at the current level.

DSA C#
while (queue.Count > 0) {
    int levelSize = queue.[1];
    var levelList = new List<int>();
Drag options to blanks, or click blank then click option'
ALength
BCapacity
CSize
DCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length which is for arrays or strings.
Using Size which is not a property in C# collections.
3fill in blank
hard

Fix the error in the code to remove the front node from the queue.

DSA C#
for (int i = 0; i < levelSize; i++) {
    var node = queue.[1]();
    levelList.Add(node.val);
Drag options to blanks, or click blank then click option'
APop
BRemove
CDequeue
DPoll
Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop() which is for Stack.
Using Remove() which requires an argument.
4fill in blank
hard

Fill both blanks to add child nodes to the queue if they exist.

DSA C#
if (node.[1] != null) queue.[2](node.left);
if (node.right != null) queue.Enqueue(node.right);
Drag options to blanks, or click blank then click option'
Aleft
Bright
CEnqueue
DPush
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.right instead of node.left in the first if.
Using Push() which is for Stack.
5fill in blank
hard

Fill both blanks to reverse the level list on odd levels and add it to the result.

DSA C#
if (isLeftToRight) {
    result.Add(levelList);
} else {
    levelList.[1]();
    result.[2](levelList);
}
isLeftToRight = !isLeftToRight;
Drag options to blanks, or click blank then click option'
AReverse
BAdd
CAddRange
DSort
Attempts:
3 left
💡 Hint
Common Mistakes
Using Sort() which changes order but not to zigzag.
Using AddRange() which adds elements individually, not as a list.

Practice

(1/5)
1. What does the Zigzag Level Order Traversal of a binary tree do differently compared to a normal level order traversal?
easy
A. It alternates the direction of traversal at each level, left to right then right to left.
B. It visits nodes only on the left subtree.
C. It visits nodes in reverse order of their insertion.
D. It traverses nodes randomly without any order.

Solution

  1. Step 1: Understand normal level order traversal

    Normal level order traversal visits nodes level by level from left to right.
  2. Step 2: Identify zigzag difference

    Zigzag traversal alternates direction each level: first left to right, then right to left, and so on.
  3. Final Answer:

    It alternates the direction of traversal at each level, left to right then right to left. -> Option A
  4. Quick Check:

    Zigzag = alternate directions [OK]
Hint: Remember zigzag means direction flips every level [OK]
Common Mistakes:
  • Thinking it only visits left subtree
  • Assuming traversal is always left to right
  • Confusing zigzag with reverse order
2. Which of the following C# code snippets correctly initializes a queue to perform a zigzag level order traversal?
easy
A. List<TreeNode> queue = new List<TreeNode>();
B. Queue<TreeNode> queue = new Queue<TreeNode>();
C. Stack<TreeNode> queue = new Stack<TreeNode>();
D. Dictionary queue = new Dictionary();

Solution

  1. Step 1: Identify data structure for BFS

    Queue is used for breadth-first search to process nodes level by level.
  2. Step 2: Check code snippet correctness

    Queue<TreeNode> queue = new Queue<TreeNode>(); correctly declares a Queue of TreeNode objects, suitable for traversal.
  3. Final Answer:

    Queue<TreeNode> queue = new Queue<TreeNode>(); -> Option B
  4. Quick Check:

    Queue for BFS = Queue<TreeNode> queue = new Queue<TreeNode>(); [OK]
Hint: Use Queue for level order traversal in C# [OK]
Common Mistakes:
  • Using Stack instead of Queue for BFS
  • Using List which lacks FIFO behavior
  • Using Dictionary which is not suitable here
3. Given the binary tree:
    1
   / \
  2   3
 / \   \
4   5   6

What is the output of zigzag level order traversal?
medium
A. [[1], [3, 2], [4, 5, 6]]
B. [[1], [2, 3], [4, 5, 6]]
C. [[1], [3, 2], [6, 5, 4]]
D. [[1, 2, 3], [4, 5, 6]]

Solution

  1. Step 1: Traverse level 0 left to right

    Level 0 nodes: [1]
  2. Step 2: Traverse level 1 right to left

    Level 1 nodes: [2, 3], reversed to [3, 2]
  3. Step 3: Traverse level 2 left to right

    Level 2 nodes: [4, 5, 6]
  4. Final Answer:

    [[1], [3, 2], [4, 5, 6]] -> Option A
  5. Quick Check:

    Zigzag levels = [[1], [3, 2], [4, 5, 6]] [OK]
Hint: Alternate direction each level, reverse nodes on odd levels [OK]
Common Mistakes:
  • Not reversing nodes on alternate levels
  • Mixing node order in last level
  • Confusing left and right child order
4. Consider this C# code snippet for zigzag traversal:
if (level % 2 == 0)
    result.Add(currentLevel);
else
    result.Add(currentLevel.Reverse().ToList());

What is the bug in this code?
medium
A. It reverses the list on even levels instead of odd levels.
B. It reverses the list on odd levels instead of even levels.
C. It reverses the original list causing side effects.
D. It does not reverse the list at all.

Solution

  1. Step 1: Understand zigzag pattern

    Zigzag reverses nodes on odd levels (1, 3, 5...), not even levels (0, 2, 4...).
  2. Step 2: Analyze code condition

    The condition correctly identifies even/odd levels for reversal.
  3. Step 3: Identify the actual issue

    currentLevel.Reverse() (List<TreeNode>.Reverse) modifies the list in place and returns void, causing side effects and a compilation error on .ToList().
  4. Final Answer:

    It reverses the original list causing side effects. -> Option C
  5. Quick Check:

    List.Reverse() in-place modifies original [OK]
Hint: Reverse nodes only on odd levels (1, 3, 5...) [OK]
Common Mistakes:
  • Reversing on even levels instead of odd
  • Modifying original list causing bugs
  • Not reversing at all
5. You want to perform zigzag level order traversal on a binary tree but only collect nodes with even values. Which modification is best to apply?
hard
A. Use a stack instead of a queue to collect only even nodes.
B. Reverse the entire result list after traversal to get even nodes first.
C. Skip reversing on odd levels to keep order consistent for even nodes.
D. Filter nodes by even value before adding them to the current level list during traversal.

Solution

  1. Step 1: Identify filtering requirement

    We want only even-valued nodes in the output, so filter nodes during traversal.
  2. Step 2: Apply filtering during level collection

    Filter nodes by checking if value is even before adding to current level list.
  3. Step 3: Keep zigzag logic intact

    After filtering, apply zigzag reversal as usual on filtered nodes.
  4. Final Answer:

    Filter nodes by even value before adding them to the current level list during traversal. -> Option D
  5. Quick Check:

    Filter during traversal = Filter nodes by even value before adding them to the current level list during traversal. [OK]
Hint: Filter nodes while collecting each level, then zigzag [OK]
Common Mistakes:
  • Reversing entire result instead of filtering
  • Skipping reversal breaks zigzag pattern
  • Using stack incorrectly for filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard