Count Total Nodes in Binary Tree in DSA C# - Time & Space Complexity
We want to understand how long it takes to count all nodes in a binary tree.
How does the time needed grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
public int CountNodes(TreeNode root) {
if (root == null) return 0;
return 1 + CountNodes(root.left) + CountNodes(root.right);
}
This code counts all nodes by visiting each node once recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call visiting each node once.
- How many times: Once per node in the tree.
Each node is visited exactly one time, 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: The time grows in a straight line as the tree size increases.
Time Complexity: O(n)
This means the time to count nodes grows directly with the number of nodes in the tree.
[X] Wrong: "The recursion only visits some nodes, so it is faster than O(n)."
[OK] Correct: The function must visit every node to count it, so it always touches all nodes once.
Counting nodes in a tree is a basic skill that shows you understand recursion and tree traversal well.
"What if we changed the tree to a linked list (all nodes have only one child)? How would the time complexity change?"
Practice
Solution
Step 1: Understand the node counting concept
The function counts every node in the tree, including root and all children.Step 2: Differentiate from partial counts
Counting only leaves or root ignores other nodes, so total count includes all nodes.Final Answer:
Counts all nodes including root, left, and right children -> Option DQuick Check:
Total nodes = all nodes in tree [OK]
- Counting only leaf nodes
- Ignoring root node
- Counting only one subtree
Solution
Step 1: Check class and member access
public class Node { public int data; public Node left, right; } correctly defines a public class with public members for data and child nodes.Step 2: Verify syntax correctness
class Node { int data; Node left, right; void count() {} } misses access modifiers and method is irrelevant; C uses struct which is less common; D has private right node which restricts access.Final Answer:
public class Node { public int data; public Node left, right; } -> Option AQuick Check:
Correct class with public members [OK]
- Missing public keyword
- Using struct instead of class
- Private child nodes blocking access
class Node {
public int data;
public Node left, right;
public Node(int val) { data = val; left = right = null; }
}
int CountNodes(Node root) {
if (root == null) return 0;
return 1 + CountNodes(root.left) + CountNodes(root.right);
}
// Tree:
// 1
// / \
// 2 3
// /
// 4
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
Console.WriteLine(CountNodes(root));Solution
Step 1: Count nodes in the tree structure
The tree has nodes: 1 (root), 2 (left child), 3 (right child), and 4 (left child of 2), total 4 nodes.Step 2: Apply recursive count function
CountNodes returns 1 + left subtree count + right subtree count, summing to 4.Final Answer:
4 -> Option AQuick Check:
Nodes counted = 4 [OK]
- Counting only children, missing root
- Miscounting null nodes
- Adding extra 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 incorrect; it should be 0 because null means no node.Step 2: Check counting logic for current node
Function does not add 1 for the current root node, so it misses counting it.Step 3: Combine errors
Both base case and missing addition cause wrong total count.Final Answer:
All of the above -> Option CQuick Check:
Base case and counting errors combined [OK]
- Returning 1 for null node
- Not adding 1 for current node
- Ignoring base case correctness
Solution
Step 1: Define condition for counting nodes
We want to count nodes that have at least one child, so check if left or right child is not null.Step 2: Modify recursive function accordingly
Recursively count nodes where left or right child exists, ignoring leaf nodes with no children.Step 3: Implement logic
Return 1 + counts of children if node has child; else 0 for leaf nodes.Final Answer:
Count nodes where left or right child is not null, recursively -> Option BQuick Check:
Count nodes with children only [OK]
- Counting all nodes including leaves
- Counting only leaves
- Counting nodes with no children
Ready to prove it?
Pass the test and this pattern moves unknown
