Count Total Nodes in Binary Tree in DSA C - Time & Space Complexity
When counting all nodes in a binary tree, we want to know how the work grows as the tree gets bigger.
How does the time needed change when the tree has more nodes?
Analyze the time complexity of the following code snippet.
int countNodes(struct Node* root) {
if (root == NULL) {
return 0;
}
return 1 + countNodes(root->left) + countNodes(root->right);
}
This code counts all nodes by visiting each node once, adding 1 for the current node plus counts from left and right subtrees.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Once per node in the tree.
Each node is visited exactly once, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: Doubling the nodes roughly doubles the work.
Time Complexity: O(n)
This means the time grows linearly with the number of nodes in the tree.
[X] Wrong: "The function only visits some nodes, so it runs faster than O(n)."
[OK] Correct: The function must visit every node to count them, so it always touches all nodes, making the time proportional to n.
Understanding this linear time complexity helps you explain how recursive tree traversals work and why visiting all nodes is necessary for counting.
"What if we only counted nodes at a certain depth? How would the time complexity change?"
Practice
Solution
Step 1: Understand the function purpose
The function visits every node in the tree to count them all.Step 2: Recognize node types counted
It includes root, internal nodes, and leaf nodes in the count.Final Answer:
Counts all nodes including root, left, and right children -> Option AQuick Check:
Total nodes = all nodes in tree [OK]
- Counting only leaf nodes
- Ignoring root node in count
- Counting only nodes with two children
Solution
Step 1: Identify base case for recursion
When the node is NULL, it means no node exists, so count is zero.Step 2: Confirm correct return value
Return 0 to indicate no nodes to count at this branch.Final Answer:
if (root == NULL) return 0; -> Option CQuick Check:
NULL node means zero nodes [OK]
- Returning 1 for NULL node
- Checking only left or right child for base case
- Missing base case causing infinite recursion
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int countNodes(Node* root) {
if (root == NULL) return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}
// Tree structure:
// 10
// / \
// 5 15
// / / \
// 3 12 20
int main() {
Node n3 = {3, NULL, NULL};
Node n5 = {5, &n3, NULL};
Node n12 = {12, NULL, NULL};
Node n20 = {20, NULL, NULL};
Node n15 = {15, &n12, &n20};
Node root = {10, &n5, &n15};
printf("%d", countNodes(&root));
return 0;
}Solution
Step 1: Count nodes in left subtree
Left subtree nodes: 5 and 3 -> 2 nodes.Step 2: Count nodes in right subtree
Right subtree nodes: 15, 12, 20 -> 3 nodes.Step 3: Add root node
Root node 10 adds 1 more node.Final Answer:
6 -> Option BQuick Check:
2 + 3 + 1 = 6 [OK]
- Counting only leaf nodes
- Forgetting to add root node
- Miscounting subtree nodes
int countNodes(Node* root) {
if (root == NULL) return 1;
return countNodes(root->left) + countNodes(root->right);
}Solution
Step 1: Check base case return value
Returning 1 for NULL node is wrong; should return 0.Step 2: Check counting current node
Function misses counting current node; add +1 to sum of children.Final Answer:
Both A and B are needed -> Option AQuick Check:
Base case 0 + count current node 1 [OK]
- Returning 1 for NULL node
- Not adding 1 for current node
- Assuming function is correct without base case
Solution
Step 1: Identify nodes with children
Check if node's left or right child is not NULL to confirm it has children.Step 2: Count only nodes with children
Return 1 for nodes with children plus recursive counts; skip leaf nodes.Final Answer:
Count nodes where left or right child is not NULL, skip leaf nodes -> Option DQuick Check:
Count nodes with children only [OK]
- Counting leaf nodes mistakenly
- Counting only nodes with two children
- Ignoring nodes with one child
Ready to prove it?
Pass the test and this pattern moves unknown
