0
0
DSA C++programming~10 mins

Count Total Nodes in Binary Tree in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Count Total Nodes in Binary Tree
Start at root node
Is node null?
YesReturn 0
No
Count left subtree nodes
Count right subtree nodes
Add 1 (current node) + left count + right count
Return total count
The process starts at the root, recursively counts nodes in left and right subtrees, then adds 1 for the current node.
Execution Sample
DSA C++
int countNodes(Node* root) {
  if (root == nullptr) return 0;
  return 1 + countNodes(root->left) + countNodes(root->right);
}
This function counts all nodes in a binary tree by recursively summing nodes in left and right subtrees plus the current node.
Execution Table
StepOperationNode VisitedLeft CountRight CountTotal CountTree State
1Visit root1---1 / \ 2 3
2Visit left child2---1 / \ 2 3
3Visit left child's left child4---1 / \ 2 3 / 4
4Left child of 4 is nullnull0--1 / \ 2 3 / 4
5Right child of 4 is nullnull-0-1 / \ 2 3 / 4
6Count nodes at 440011 / \ 2 3 / 4
7Visit right child of 25---1 / \ 2 3 / \ 4 5
8Left child of 5 is nullnull0--1 / \ 2 3 / \ 4 5
9Right child of 5 is nullnull-0-1 / \ 2 3 / \ 4 5
10Count nodes at 550011 / \ 2 3 / \ 4 5
11Count nodes at 221131 / \ 2 3 / \ 4 5
12Visit right child3---1 / \ 2 3 / \ 4 5
13Left child of 3 is nullnull0--1 / \ 2 3 / \ 4 5
14Right child of 3 is nullnull-0-1 / \ 2 3 / \ 4 5
15Count nodes at 330011 / \ 2 3 / \ 4 5
16Count nodes at root13151 / \ 2 3 / \ 4 5
17Return total count---51 / \ 2 3 / \ 4 5
💡 All nodes visited; recursion ends when null nodes are reached returning 0.
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 10After Step 11After Step 15After Step 16Final
Current Node1445231-
Left Count-000103-
Right Count--00101-
Total Count--113155
Key Moments - 3 Insights
Why do we return 0 when the node is null?
Returning 0 at null nodes (see Step 4 and Step 8) stops recursion and counts no nodes beyond leaf ends.
How does the function add counts from left and right subtrees?
At each node (e.g., Step 11), the function adds 1 (current node) plus counts returned from left and right recursive calls.
Why does the total count at root equal 5?
Because it sums all nodes counted in left subtree (3), right subtree (1), plus itself (1), as shown in Step 16.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the total count after visiting node 2 (Step 11)?
A2
B3
C1
D5
💡 Hint
Check the 'Total Count' column at Step 11 in the execution table.
At which step does the function return 0 for a null node?
AStep 6
BStep 11
CStep 4
DStep 16
💡 Hint
Look for steps where 'Node Visited' is 'null' and 'Left Count' or 'Right Count' is 0.
If the right subtree of node 1 had one more node, how would the total count at root (Step 16) change?
AIt would increase by 1
BIt would stay the same
CIt would decrease by 1
DIt would double
💡 Hint
Total count sums all nodes; adding one node in right subtree adds 1 to total count at root.
Concept Snapshot
Count Total Nodes in Binary Tree:
- Use recursion starting at root
- If node is null, return 0
- Else return 1 + count(left) + count(right)
- Counts all nodes including root
- Stops when leaf children are null
Full Transcript
This concept shows how to count all nodes in a binary tree using recursion. Starting at the root, the function visits each node, counts nodes in the left subtree, counts nodes in the right subtree, then adds 1 for the current node. When a null node is reached, it returns 0 to stop recursion. The execution table traces each step visiting nodes 1, 2, 4, 5, and 3, showing counts returned from subtrees and total counts computed. The variable tracker shows how counts accumulate. Key moments clarify why null nodes return 0, how counts add up, and why the final total is 5. The visual quiz tests understanding of counts at specific steps and effects of adding nodes. This method ensures every node is counted exactly once.