Height of Binary Tree in DSA Typescript - Time & Space Complexity
We want to know how long it takes to find the height of a binary tree as the tree grows bigger.
The question is: how does the time needed change when the tree has more nodes?
Analyze the time complexity of the following code snippet.
function heightOfBinaryTree(root: TreeNode | null): number {
if (root === null) return 0;
const leftHeight = heightOfBinaryTree(root.left);
const rightHeight = heightOfBinaryTree(root.right);
return Math.max(leftHeight, rightHeight) + 1;
}
This code finds the height by checking the height of left and right subtrees recursively and returning the larger one plus one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Each node in the tree is visited exactly one time.
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 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The time grows linearly as the tree size increases.
Time Complexity: O(n)
This means the time to find the height grows in direct proportion to the number of nodes in the tree.
[X] Wrong: "The height calculation only depends on the longest path, so it should be faster than visiting all nodes."
[OK] Correct: Even though height depends on the longest path, the function must check every node to be sure no taller path exists, so it visits all nodes.
Understanding this helps you explain how recursive tree algorithms work and how their time depends on tree size, a common topic in interviews.
"What if the tree was balanced versus very skewed? How would the time complexity change?"
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 AQuick 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: Identify the correct parameter type for a tree node
The function must accept a node or null (for empty subtree), so type is TreeNode | null.Step 2: Confirm the return type is number
The height is a numeric value, so the return type must be number.Final Answer:
function height(node: TreeNode | null): number -> Option AQuick Check:
Parameter: TreeNode|null, Return: number [OK]
- Using wrong parameter type like number instead of node
- Returning void or string instead of number
- Missing null to handle empty subtree
1 / \ 2 3 / 4
Consider height as number of edges on longest path from root to leaf.
Solution
Step 1: Identify all root-to-leaf paths and count edges
Paths: 1->2->4 has 2 edges, 1->3 has 1 edge.Step 2: Find the longest path edge count
Longest path is 1->2->4 with 2 edges.Final Answer:
2 -> Option DQuick Check:
Longest path edges = 2 [OK]
- Counting nodes instead of edges
- Choosing shorter path length
- Confusing height with tree depth
function height(node: TreeNode | null): number {
if (!node) return 0;
return Math.max(height(node.left), height(node.right));
}Solution
Step 1: Analyze base case return value
Returning 0 for null means empty subtree height is 0, which is acceptable if counting edges from nodes.Step 2: Check recursive return statement
Function returns max height of children but misses adding 1 for current edge from node to child.Final Answer:
Missing +1 to count current node edge -> Option CQuick Check:
Height = max child height + 1 edge [OK]
- Forgetting to add 1 for current node edge
- Returning wrong base case value
- Using min instead of max for height
2 / \ 3 4 / / \ 6 5 8
Solution
Step 1: Identify nodes with even values and their connections
Even nodes: 2 (root), 4 (right child), 6 (left-left child), 8 (right-right child). Paths counting only even nodes:- 2 -> 4 -> 8 (edges = 2)
- 2 -> 3 (3 is odd, so path ends at 2)
- 2 -> 3 -> 6 (6 is even but 3 is odd, so path breaks at 3)
Step 2: Calculate height counting only edges between even nodes
Longest path with even nodes is 2 -> 4 -> 8 with 2 edges.Final Answer:
2 -> Option BQuick Check:
Longest even-valued node path edges = 2 [OK]
- Counting odd nodes in path
- Counting nodes instead of edges
- Ignoring breaks caused by odd nodes
Ready to prove it?
Pass the test and this pattern moves unknown
