0
0
DSA C++programming~10 mins

Tree Traversal Level Order BFS in DSA C++ - Interactive Practice

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

Complete 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'
ATreeNode*
Bint
Cstring
Dvector<int>
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of TreeNode* in the queue declaration.
Using vector which is not suitable for BFS queue.
2fill in blank
medium

Complete 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'
Apop
Bpush
Cfront
Dempty
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of push() to add elements.
Using front() which only accesses but does not add.
3fill in blank
hard

Fix 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'
Afront
Bsize
Cpop
Dempty
Attempts:
3 left
💡 Hint
Common Mistakes
Using front() or pop() in the condition which are not boolean checks.
Using size() without comparison.
4fill in blank
hard

Fill 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'
Afront
Bpop
Cpush
Dempty
Attempts:
3 left
💡 Hint
Common Mistakes
Using push() instead of pop() to remove elements.
Using empty() which only checks if queue is empty.
5fill in blank
hard

Fill 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'
Aleft
Bpush
Cright
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of push() to add children.
Checking right child but pushing left child or vice versa.