Height of Binary Tree in DSA Javascript - Time & Space Complexity
We want to know how long it takes to find the height of a binary tree as the tree grows bigger.
How does the time needed change when the tree has more nodes?
Analyze the time complexity of the following code snippet.
function height(node) {
if (node === null) return 0;
const leftHeight = height(node.left);
const rightHeight = height(node.right);
return 1 + Math.max(leftHeight, rightHeight);
}
This code finds the height of a binary tree by checking the height of left and right subtrees recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Once per node in the tree.
As the number of nodes grows, the function visits each node once, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls |
| 100 | About 100 calls |
| 1000 | About 1000 calls |
Pattern observation: The time grows linearly with the number of nodes.
Time Complexity: O(n)
This means the time to find the height grows directly with the number of nodes in the tree.
[X] Wrong: "The height function only checks one path, so it runs in constant time."
[OK] Correct: The function must visit every node to find the height, not just one path, so it takes time proportional to all nodes.
Understanding how recursive tree functions scale helps you explain your solutions clearly and shows you know how to handle common data structures.
"What if the tree was balanced vs very unbalanced? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand the definition of height in a binary tree
The height is the longest path from the root node down to the farthest leaf node, counted by edges.Step 2: Differentiate height from other tree properties
Number of nodes or leaf nodes are different concepts; height specifically counts edges on the longest path.Final Answer:
The number of edges on the longest path from root to a leaf -> Option DQuick Check:
Height = longest root-to-leaf edge count [OK]
- Confusing height with total nodes count
- Counting nodes instead of edges for height
- Thinking height equals number of leaf nodes
Solution
Step 1: Understand the base case for empty subtree
When the node is null (no node), height is defined as -1 to count edges properly.Step 2: Why -1 instead of 0?
Returning -1 for null ensures leaf nodes have height 0 (0 edges below them), matching edge count definition.Final Answer:
if (node === null) return -1; -> Option AQuick Check:
Empty node height = -1 for correct edge count [OK]
- Returning 0 for null node causes off-by-one height
- Checking undefined instead of null for nodes
- Confusing node absence with leaf node
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
function height(node) {
if (node === null) return -1;
return 1 + Math.max(height(node.left), height(node.right));
}
const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.left.left = new Node(1);
console.log(height(root));Solution
Step 1: Trace the tree structure and count edges
The longest path is from root (10) to left-left-left (1): edges are 10->5, 5->3, 3->1, total 3 edges.Step 2: Calculate height using the function logic
Function returns 1 + max height of children. For leaf node (1), height = 0. So total height = 3 edges means function returns 3.Final Answer:
3 -> Option BQuick Check:
Longest path edges = 3 [OK]
- Counting nodes instead of edges for height
- Misreading tree structure and missing deepest node
- Confusing return value for leaf node height
function height(node) {
if (node === null) return 0;
return Math.max(height(node.left), height(node.right));
}Solution
Step 1: Analyze the base case and recursive return
Base case returns 0 for null, which is acceptable if counting nodes, but function misses adding 1 for current node.Step 2: Identify missing increment for current node
Function returns max height of children but does not add 1 for current node, so height is underestimated by 1.Final Answer:
Add +1 before Math.max to count current node -> Option CQuick Check:
Height = 1 + max(child heights) [OK]
- Forgetting to add 1 for current node
- Changing base case unnecessarily
- Returning sum instead of max height
function height(node) {
if (node === null) return ?;
return ? + Math.max(height(node.left), height(node.right));
}Solution
Step 1: Understand difference between counting edges and nodes
Counting nodes means height of empty tree is 0, and each node adds 1 to path length.Step 2: Adjust base case and recursive addition accordingly
Return 0 for null node (no nodes), and add 1 for current node to count nodes on path.Final Answer:
Return 0 for null; add 1 before Math.max to count nodes -> Option AQuick Check:
Height counts nodes: base=0, add 1 per node [OK]
- Using -1 base for node count causes off-by-one
- Not adding 1 for current node
- Confusing edges count with nodes count
Ready to prove it?
Pass the test and this pattern moves unknown
