0
0
DSA Typescriptprogramming~10 mins

Count Total Nodes in Binary Tree in DSA Typescript - 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
Sum left + right + 1 (current node)
Return total count
The process visits each node starting from the root, counts nodes in left and right subtrees recursively, then sums them with the current node.
Execution Sample
DSA Typescript
function countNodes(node: TreeNode | null): number {
  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 in left and right subtrees plus the current node.
Execution Table
StepOperationNode VisitedLeft Subtree CountRight Subtree CountTotal CountVisual State
1Start at root1---1 / \ 2 3
2Go left2---1 / 2
3Go left4---4 (leaf)
4Left nullnull0--null
5Right nullnull-0-null
6Count node 440014 (leaf) counted
7Back to 2, go right5---5 (leaf)
8Left nullnull0--null
9Right nullnull-0-null
10Count node 550015 (leaf) counted
11Count node 221132 subtree counted
12Back to root, go right3---3 (leaf)
13Left nullnull0--null
14Right nullnull-0-null
15Count node 330013 (leaf) counted
16Count root node 11315Full tree counted
17End----Total nodes = 5
💡 All nodes visited; recursion ends when null nodes are reached.
Variable Tracker
VariableStartAfter Step 6After Step 10After Step 11After Step 15After Step 16Final
node1 (root)4 (leaf)5 (leaf)2 (subtree root)3 (leaf)1 (root)null (end)
left subtree count-001033
right subtree count-001011
total count-113155
Key Moments - 3 Insights
Why do we return 0 when the node is null?
Returning 0 at null nodes (see steps 4,5,8,9,13,14) means no nodes exist there, so they don't add to the count.
How does the function count nodes in left and right subtrees separately?
The function calls itself recursively on node.left and node.right (steps 2,3 for left; steps 12,13 for right), counting nodes in each subtree before summing.
Why do we add 1 after counting left and right subtrees?
Adding 1 (step 16) accounts for the current node itself, ensuring it is included in the total count.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the total count after counting node 2 (step 11)?
A1
B5
C3
D0
💡 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 on the right subtree of node 3?
AStep 13
BStep 14
CStep 15
DStep 16
💡 Hint
Look for 'Right null' operation on node 3 in the execution_table.
If the tree had no right child for the root, how would the total count at step 16 change?
AIt would be 4
BIt would be 5
CIt would be 3
DIt would be 1
💡 Hint
Consider the right subtree count column and how missing right child affects total count.
Concept Snapshot
Count Total Nodes in Binary Tree:
- Use recursion starting at root
- If node is null, return 0
- Count nodes in left subtree
- Count nodes in right subtree
- Return sum of left + right + 1 (current node)
- Visits all nodes exactly once
Full Transcript
To count total nodes in a binary tree, start at the root node. If the node is null, return 0 because there are no nodes to count. Otherwise, recursively count nodes in the left subtree and right subtree. Add these counts together and add 1 for the current node. This process repeats until all nodes are visited. The final sum is the total number of nodes in the tree.