Count Total Nodes in Binary Tree in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to count all nodes in a binary tree.
How does the time grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
function countNodes(root) {
if (root === null) return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
}
This code counts all nodes by visiting each node once using recursion.
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 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 to count nodes grows linearly with the number of nodes in the tree.
[X] Wrong: "The recursion might visit some nodes multiple times, so it could be slower than linear."
[OK] Correct: Each node is visited exactly once because the function calls only go down the tree branches without repeats.
Understanding this linear time complexity helps you explain how recursive tree traversals work efficiently in real problems.
"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 function's purpose
The function is designed to count every node in the binary tree, including root, internal, and leaf nodes.Step 2: Differentiate from other tree properties
Counting nodes is different from calculating height, sum of values, or counting leaves, which are other tree properties.Final Answer:
It returns the total number of nodes in the tree. -> Option AQuick Check:
Counting nodes = total nodes [OK]
- Confusing node count with tree height
- Counting only leaf nodes
- Summing node values instead of counting
Solution
Step 1: Identify the base case for recursion
When the node is null (no node), the count should be zero because there is no node to count.Step 2: Check correct JavaScript syntax
Using strict equality (===) to check for null and returning 0 is the correct approach.Final Answer:
if (node === null) return 0; -> Option BQuick Check:
Base case null node returns 0 [OK]
- Returning 1 for null nodes
- Using == instead of ===
- Returning null instead of 0
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
function countNodes(node) {
if (node === null) return 0;
return 1 + countNodes(node.left) + countNodes(node.right);
}
console.log(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 nodes = 4.Step 2: Apply recursive countNodes function
Function counts 1 for current node plus counts of left and right subtrees recursively, resulting in 4.Final Answer:
4 -> Option DQuick Check:
Nodes counted = 4 [OK]
- Forgetting to count the root node
- Miscounting child nodes
- Confusing left and right subtree counts
function countNodes(node) {
if (node == null) return 1;
return 1 + countNodes(node.left) + countNodes(node.right);
}Solution
Step 1: Check base case return value
Base case returns 1 when node is null, which incorrectly counts non-existent nodes.Step 2: Understand correct base case
Base case should return 0 for null nodes because they do not exist and should not add to count.Final Answer:
Base case returns 1 instead of 0 for null node. -> Option AQuick Check:
Null node count must be 0 [OK]
- Returning 1 for null nodes
- Not adding 1 for current node
- Confusing equality operators
function countNodes(node) {
if (node === null) return 0;
return 1 + countNodes(node.left) + countNodes(node.right);
}Solution
Step 1: Analyze current function behavior
The function returns 0 for null nodes and adds 1 for each existing node plus counts of left and right children recursively.Step 2: Confirm it handles missing children
Since null children return 0, missing children do not add to count, so all existing nodes are counted correctly.Final Answer:
No change needed; function already counts all nodes correctly. -> Option CQuick Check:
Function counts all nodes including those with missing children [OK]
- Thinking missing children need special counting
- Changing base case incorrectly
- Using global counters unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
