Challenge - 5 Problems
Binary Tree Node Counting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Count each node including root and all children recursively.
✗ Incorrect
The tree has nodes: 10 (root), 20, 30, 40, 50. Total nodes = 5.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Count all nodes along the right side only.
✗ Incorrect
The tree has nodes 1, 2, 3, 4 all on the right side. Total nodes = 4.
🧠 Conceptual
advanced1:30remaining
Understanding Node Counting Complexity
What is the time complexity of counting total nodes in a binary tree using a recursive approach?
Attempts:
2 left
💡 Hint
Each node is visited exactly once.
✗ Incorrect
The recursive function visits every node once, so time complexity is linear O(n).
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the base case condition carefully.
✗ Incorrect
Base case recurses on nullptr, causing infinite recursion and stack overflow.
🚀 Application
expert2: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; }
Attempts:
2 left
💡 Hint
Add children nodes to the queue only if they exist.
✗ Incorrect
Option B correctly adds left and right children if they exist, ensuring all nodes are counted.