Challenge - 5 Problems
Zigzag Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Zigzag Level Order Traversal on a small tree
What is the output of the following C# code performing zigzag level order traversal on the given binary tree?
DSA C#
public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { this.val = val; this.left = left; this.right = right; } } public IList<IList<int>> ZigzagLevelOrder(TreeNode root) { var results = new List<IList<int>>(); if (root == null) return results; var currentLevel = new List<TreeNode> { root }; bool leftToRight = true; while (currentLevel.Count > 0) { var levelValues = new List<int>(); var nextLevel = new List<TreeNode>(); foreach (var node in currentLevel) { levelValues.Add(node.val); if (node.left != null) nextLevel.Add(node.left); if (node.right != null) nextLevel.Add(node.right); } if (!leftToRight) levelValues.Reverse(); results.Add(levelValues); currentLevel = nextLevel; leftToRight = !leftToRight; } return results; } // Tree structure: // 1 // / \ // 2 3 // / \ \ // 4 5 6 var root = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3, null, new TreeNode(6)) ); var result = ZigzagLevelOrder(root); foreach(var level in result) { Console.WriteLine(string.Join(",", level)); }
Attempts:
2 left
💡 Hint
Remember that the traversal direction alternates at each level.
✗ Incorrect
The traversal starts left to right at level 1: [1].
Next level is right to left: [3, 2].
Next level is left to right again: [4, 5, 6].
❓ Predict Output
intermediateOutput for Zigzag Traversal on single node tree
What is the output of zigzag level order traversal on a tree with only one node (value 10)?
DSA C#
var root = new TreeNode(10); var result = ZigzagLevelOrder(root); foreach(var level in result) { Console.WriteLine(string.Join(",", level)); }
Attempts:
2 left
💡 Hint
A single node tree has only one level.
✗ Incorrect
The traversal returns one level with the single node value 10.
🔧 Debug
advancedIdentify the error in zigzag traversal code
What error will this code produce when run on a non-empty tree?
DSA C#
public IList<IList<int>> ZigzagLevelOrder(TreeNode root) { var results = new List<IList<int>>(); if (root == null) return results; var queue = new Queue<TreeNode>(); queue.Enqueue(root); bool leftToRight = true; while (queue.Count > 0) { int size = queue.Count; var levelValues = new List<int>(); for (int i = 0; i < size; i++) { var node = queue.Dequeue(); if (leftToRight) { levelValues.Add(node.val); } else { levelValues.Insert(0, node.val); } if (node.left != null) queue.Enqueue(node.left); if (node.right != null) queue.Enqueue(node.right); } results.Add(levelValues); leftToRight = !leftToRight; } return results; } // Usage with a tree root
Attempts:
2 left
💡 Hint
Check how the queue is managed and how nodes are enqueued and dequeued.
✗ Incorrect
The code correctly uses a queue and alternates insertion order to achieve zigzag traversal without errors.
🚀 Application
advancedNumber of levels in zigzag traversal output
Given a complete binary tree with 7 nodes, how many levels will the zigzag level order traversal output contain?
Attempts:
2 left
💡 Hint
Count the height of a complete binary tree with 7 nodes.
✗ Incorrect
A complete binary tree with 7 nodes has height 3, so traversal output has 3 levels.
🧠 Conceptual
expertWhy use a deque or list reversal in zigzag traversal?
In zigzag level order traversal, why do we reverse the list of node values or use a deque to insert at front on alternate levels?
Attempts:
2 left
💡 Hint
Think about the zigzag pattern and how order changes per level.
✗ Incorrect
Reversing or inserting at front changes the order of nodes to achieve the zigzag pattern alternating directions.
