Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Add() which is for List, not Queue.
Using Push() which is for Stack.
✗ Incorrect
In C#, Queue uses Enqueue() to add elements at the end.
2fill in blank
mediumComplete 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>();
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length which is for arrays or strings.
Using Size which is not a property in C# collections.
✗ Incorrect
Queue has a Count property to get the number of elements.
3fill in blank
hardFix 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);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop() which is for Stack.
Using Remove() which requires an argument.
✗ Incorrect
Queue uses Dequeue() to remove and return the front element.
4fill in blank
hardFill 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);
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.right instead of node.left in the first if.
Using Push() which is for Stack.
✗ Incorrect
Check node.left for null and add it using Enqueue().
5fill in blank
hardFill 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;
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.
✗ Incorrect
Reverse the list, then add it to result using Add().
