0
0
DSA C++programming~20 mins

Count Total Nodes in Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Tree Node Counting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Node Count in a Simple Binary Tree
What is the output of the following C++ code that counts total nodes in a binary tree?
DSA C++
#include <iostream>

struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

int countNodes(Node* root) {
    if (!root) return 0;
    return 1 + countNodes(root->left) + countNodes(root->right);
}

int main() {
    Node* root = new Node(10);
    root->left = new Node(20);
    root->right = new Node(30);
    root->left->left = new Node(40);
    root->left->right = new Node(50);
    int total = countNodes(root);
    std::cout << total << std::endl;
    return 0;
}
A5
B4
C3
D6
Attempts:
2 left
💡 Hint
Count each node including root and all children recursively.
Predict Output
intermediate
2:00remaining
Count Nodes in a Skewed Binary Tree
What will be the output of the node count function on this skewed binary tree?
DSA C++
#include <iostream>

struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

int countNodes(Node* root) {
    if (!root) return 0;
    return 1 + countNodes(root->left) + countNodes(root->right);
}

int main() {
    Node* root = new Node(1);
    root->right = new Node(2);
    root->right->right = new Node(3);
    root->right->right->right = new Node(4);
    int total = countNodes(root);
    std::cout << total << std::endl;
    return 0;
}
A4
B3
C5
D2
Attempts:
2 left
💡 Hint
Count all nodes along the right side only.
🧠 Conceptual
advanced
1:30remaining
Understanding Node Counting Complexity
What is the time complexity of counting total nodes in a binary tree using a recursive approach?
AO(n^2), where n is the number of nodes
BO(log n), where n is the number of nodes
CO(n), where n is the number of nodes
DO(1), constant time
Attempts:
2 left
💡 Hint
Each node is visited exactly once.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Node Counting Function
What error will this code produce when counting nodes in a binary tree?
DSA C++
#include <iostream>

struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

int countNodes(Node* root) {
    if (root == nullptr) return countNodes(root);
    return 1 + countNodes(root->left) + countNodes(root->right);
}

int main() {
    Node* root = new Node(5);
    root->left = new Node(3);
    root->right = new Node(7);
    int total = countNodes(root);
    std::cout << total << std::endl;
    return 0;
}
AOutputs 3
BOutputs 5
COutputs 1
DStack overflow due to infinite recursion
Attempts:
2 left
💡 Hint
Check the base case condition carefully.
🚀 Application
expert
2:30remaining
Count Nodes in a Complete Binary Tree Using Iteration
Which iterative approach correctly counts total nodes in a binary tree without recursion?
DSA C++
#include <queue>

struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

int countNodesIterative(Node* root) {
    if (!root) return 0;
    std::queue<Node*> q;
    q.push(root);
    int count = 0;
    while (!q.empty()) {
        Node* current = q.front();
        q.pop();
        count++;
        // Fill in the missing code here
    }
    return count;
}
Aq.push(current->left); q.push(current->right);
Bif (current->left) q.push(current->left); if (current->right) q.push(current->right);
Cif (current->left) q.push(current->right); if (current->right) q.push(current->left);
Dif (current->left == nullptr && current->right == nullptr) break;
Attempts:
2 left
💡 Hint
Add children nodes to the queue only if they exist.