0
0
DSA Javascriptprogramming~10 mins

Count Total Nodes in Binary Tree in DSA Javascript - 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, checks if the node is null, then recursively counts nodes in left and right subtrees, adding 1 for the current node.
Execution Sample
DSA Javascript
function countNodes(node) {
  if (node === null) return 0;
  return 1 + countNodes(node.left) + countNodes(node.right);
}
This function counts all nodes in a binary tree by recursively summing nodes from left and right children plus the current node.
Execution Table
StepOperationNode VisitedLeft CountRight CountTotal CountTree State
1Visit rootA---A / \ B C
2Visit left childB---A / \ B C
3Visit B's left childD---A / \ B C / D
4D is leaf, left nullnull0--A / \ B C / D
5D is leaf, right nullnull-0-A / \ B C / D
6Count D nodesD001A / \ B C / D
7Visit B's right childnull0--A / \ B C / D
8Count B nodesB102A / \ B C / D
9Visit right childC---A / \ B C / D
10Visit C's left childnull0--A / \ B C / D
11Visit C's right childE---A / \ B C / D \ E
12E is leaf, left nullnull0--A / \ B C / D \ E
13E is leaf, right nullnull-0-A / \ B C / D \ E
14Count E nodesE001A / \ B C / D \ E
15Count C nodesC012A / \ B C / D \ E
16Count root nodesA225A / \ B C / D \ E
17Return total countA225Final count: 5 nodes
💡 All nodes visited; recursion ends when null nodes are reached.
Variable Tracker
VariableStartAfter Step 6After Step 8After Step 14After Step 15After Step 16Final
nodeA (root)D (leaf)B (left child)E (leaf)C (right child)A (root)null (end)
leftCount-010022
rightCount-000122
totalCount-121255
Key Moments - 3 Insights
Why do we return 0 when the node is null?
Returning 0 at null nodes (see Step 4 and 5) stops recursion and counts no nodes beyond leaves.
How does the function add counts from left and right subtrees?
At each node (e.g., Step 16), it adds 1 (current node) plus leftCount and rightCount from recursive calls.
Why does the total count at root equal 5?
Because it sums all nodes counted in left subtree (2), right subtree (2), plus 1 for root itself (Step 16).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the totalCount at Step 8?
A1
B2
C0
D3
💡 Hint
Check the 'totalCount' column at Step 8 in the execution_table.
At which step does the function visit node E?
AStep 14
BStep 10
CStep 11
DStep 15
💡 Hint
Look for 'Visit C's right child' operation in the execution_table.
If node D had a left child, how would the totalCount at Step 6 change?
AIt would increase
BIt would decrease
CIt would remain 1
DIt would become 0
💡 Hint
Adding a child node increases the count returned at that subtree (see how counts add in execution_table).
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 at leaves with null children
Full Transcript
To count total nodes in a binary tree, start at the root node. If the node is null, return zero because there are no nodes to count. Otherwise, count nodes in the left subtree recursively, then count nodes in the right subtree recursively. Add these counts together and add one for the current node. This process repeats until all nodes are counted. The final result is the total number of nodes in the tree.