0
0
DSA C++programming~20 mins

Tree Traversal Level Order BFS in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Level Order Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Level Order Traversal on a Simple Binary Tree
What is the output of the following C++ code performing level order traversal (BFS) on the given binary tree?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

#include <iostream>
#include <queue>
#include <vector>

std::vector<int> levelOrder(Node* root) {
    std::vector<int> result;
    if (!root) return result;
    std::queue<Node*> q;
    q.push(root);
    while (!q.empty()) {
        Node* node = q.front();
        q.pop();
        result.push_back(node->val);
        if (node->left) q.push(node->left);
        if (node->right) q.push(node->right);
    }
    return result;
}

int main() {
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->left->right = new Node(5);
    std::vector<int> output = levelOrder(root);
    for (int v : output) std::cout << v << " ";
    return 0;
}
A2 3 1 4 5
B1 3 2 5 4
C4 5 2 3 1
D1 2 3 4 5
Attempts:
2 left
💡 Hint
Remember, level order traversal visits nodes level by level from left to right.
🧠 Conceptual
intermediate
1:30remaining
Number of Levels in Level Order Traversal
Given a binary tree, how many levels will the level order traversal visit for the tree below? Tree structure: - Root node 10 - Left child 20 - Right child 30 - Left child of 20 is 40 - Right child of 20 is 50 - Left child of 30 is 60
A5
B3
C4
D2
Attempts:
2 left
💡 Hint
Count the levels from root down to the deepest leaf.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Level Order Traversal Code
What error will the following C++ code produce when performing level order traversal?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

#include <iostream>
#include <queue>
#include <vector>

std::vector<int> levelOrder(Node* root) {
    std::vector<int> result;
    std::queue<Node*> q;
    q.push(root);
    while (!q.empty()) {
        Node* node = q.front();
        q.pop();
        result.push_back(node->val);
        if (node->left) q.push(node->left);
        if (node->right) q.push(node->right);
    }
    return result;
}

int main() {
    Node* root = nullptr;
    std::vector<int> output = levelOrder(root);
    for (int v : output) std::cout << v << " ";
    return 0;
}
ASegmentation fault (runtime error)
BEmpty output (no values printed)
CCompilation error due to missing header
DInfinite loop
Attempts:
2 left
💡 Hint
Check what happens when root is nullptr but still pushed into the queue.
📝 Syntax
advanced
1:30remaining
Syntax Error in Level Order Traversal Code
Which option contains the correct syntax to push children nodes into the queue during level order traversal?
DSA C++
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
Aif node->left { q.push(node->left); } if node->right { q.push(node->right); }
Bif node->left q.push(node->left); if node->right q.push(node->right);
Cif (node->left) q.push(node->left); if (node->right) q.push(node->right);
Dif (node->left) { q.push(node->left); } if (node->right) { q.push(node->right); }
Attempts:
2 left
💡 Hint
Remember C++ requires parentheses around conditions and semicolons after statements.
🚀 Application
expert
2:30remaining
Level Order Traversal Output After Node Insertions
Given an initially empty binary tree, nodes are inserted in this order: 1. Insert 10 as root 2. Insert 20 as left child of 10 3. Insert 30 as right child of 10 4. Insert 40 as left child of 20 5. Insert 50 as right child of 20 6. Insert 60 as left child of 30 What is the output of the level order traversal after all insertions?
A10 20 30 40 50 60
B20 10 30 40 50 60
C40 50 60 20 30 10
D10 30 20 60 50 40
Attempts:
2 left
💡 Hint
Level order visits nodes level by level from left to right.