Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a queue for BFS traversal.
DSA C#
Queue<TreeNode> queue = new [1]<TreeNode>();Attempts:
3 left
💡 Hint
Common Mistakes
Using Stack instead of Queue causes wrong traversal order.
Using List or Dictionary does not provide FIFO behavior.
✗ Incorrect
We use a Queue to hold nodes for level order traversal (BFS).
2fill in blank
mediumComplete the code to enqueue the root node before starting BFS.
DSA C#
if (root != null) queue.[1](root);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Push or Pop which are stack operations.
Using Add which is not a queue method.
✗ Incorrect
We add the root node to the queue using Enqueue to start BFS.
3fill in blank
hardFix the error in the loop condition to continue BFS while queue is not empty.
DSA C#
while (queue.[1] > 0) { /* BFS logic */ }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length or Size causes compile errors.
Using Capacity checks reserved space, not current elements.
✗ Incorrect
The Count property gives the number of elements in the queue.
4fill in blank
hardFill both blanks to dequeue a node and add its children to the queue.
DSA C#
TreeNode current = queue.[1](); if (current.[2] != null) queue.Enqueue(current.left);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop which is for stacks.
Checking right child instead of left in this line.
✗ Incorrect
Use Dequeue() to remove the front node, then check left child to enqueue.
5fill in blank
hardFill all three blanks to add the right child if it exists.
DSA C#
if (current.[1] != null) queue.[2](current.[3]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Dequeue instead of Enqueue.
Checking left child instead of right here.
✗ Incorrect
Check if right child exists, then Enqueue it to the queue.
