Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a queue for BFS traversal.
DSA C++
std::queue<[1]> q; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of TreeNode* in the queue declaration.
Using vector which is not suitable for BFS queue.
✗ Incorrect
The queue should hold pointers to TreeNode objects for BFS traversal.
2fill in blank
mediumComplete the code to push the root node into the queue.
DSA C++
q.[1](root); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of push() to add elements.
Using front() which only accesses but does not add.
✗ Incorrect
We use push() to add the root node to the queue.
3fill in blank
hardFix the error in the loop condition to continue BFS while the queue is not empty.
DSA C++
while (!q.[1]()) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using front() or pop() in the condition which are not boolean checks.
Using size() without comparison.
✗ Incorrect
We check if the queue is empty using empty() method.
4fill in blank
hardFill both blanks to get the front node and then remove it from the queue.
DSA C++
TreeNode* node = q.[1](); q.[2]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using push() instead of pop() to remove elements.
Using empty() which only checks if queue is empty.
✗ Incorrect
front() gets the first element, pop() removes it from the queue.
5fill in blank
hardFill all three blanks to add the left child to the queue if it exists.
DSA C++
if (node->[1] != nullptr) q.[2](node->[3]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of push() to add children.
Checking right child but pushing left child or vice versa.
✗ Incorrect
Check if left child exists, then push it to the queue.