Height of Binary Tree in DSA C - Time & Space Complexity
We want to understand how the time to find the height of a binary tree grows as the tree gets bigger.
How does the number of steps change when the tree has more nodes?
Analyze the time complexity of the following code snippet.
int height(struct Node* root) {
if (root == NULL) {
return 0;
}
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return 1 + (leftHeight > rightHeight ? 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 tree grows, the function visits every node once to find the height.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls |
| 100 | About 100 calls |
| 1000 | About 1000 calls |
Pattern observation: The number of steps grows roughly the same as the number of nodes.
Time Complexity: O(n)
This means the time to find the height grows linearly with the number of nodes in the tree.
[X] Wrong: "The height function only checks one path, so it runs in O(log n)."
[OK] Correct: The function visits every node to compare left and right heights, so it must check the whole tree, not just one path.
Understanding this helps you explain how recursive tree algorithms work and how their time depends on tree size, a common interview topic.
"What if the tree was always balanced? How would that affect the time complexity of finding its height?"
Practice
Solution
Step 1: Understand height definition
The height of a binary tree is the number of edges on the longest path from the root to a leaf.Step 2: Apply to single node tree
With only one node (root), the longest path has length 0 edges.Final Answer:
0 -> Option AQuick Check:
Height of single node tree = 0 [OK]
- Thinking height is 1 for single node
- Confusing height with number of nodes
- Assuming height is undefined for single node
Solution
Step 1: Check parameter type for tree node
In C, a tree node is usually passed as a pointer to struct Node.Step 2: Check return type and parameter correctness
The function returns an int (height) and takes a pointer to struct Node.Final Answer:
int height(struct Node* root); -> Option CQuick Check:
Correct signature uses pointer to struct Node [OK]
- Passing struct Node by value instead of pointer
- Using void return type instead of int
- Missing struct keyword in parameter
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int height(struct Node* root) {
if (root == NULL) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
// Tree:
// 10
// / \
// 5 15
// /
// 12
int main() {
struct Node n12 = {12, NULL, NULL};
struct Node n15 = {15, &n12, NULL};
struct Node n5 = {5, NULL, NULL};
struct Node n10 = {10, &n5, &n15};
printf("%d", height(&n10));
return 0;
}Solution
Step 1: Trace left subtree height
Left subtree of root (10) has node 5 only, height = 1.Step 2: Trace right subtree height
Right subtree has nodes 15 and 12. Height is 2 (15 -> 12).Step 3: Calculate total height
Height = max(1, 2) + 1 (for root) = 3.Final Answer:
3 -> Option DQuick Check:
Height = 3 [OK]
- Counting edges instead of nodes
- Ignoring one subtree height
- Off-by-one error in return statement
int height(struct Node* root) {
if (root == NULL) return -1;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}Solution
Step 1: Understand base case meaning
Base case returns height for empty tree. Returning -1 means height counts edges, not nodes.Step 2: Fix base case for node count height
To count nodes, base case should return 0 for NULL.Final Answer:
Change base case return from -1 to 0 -> Option BQuick Check:
Base case 0 for NULL node height [OK]
- Returning -1 causes off-by-one height
- Removing +1 leads to wrong height
- Swapping left and right recursive calls unnecessary
Solution
Step 1: Understand full binary tree properties
A full binary tree with all nodes having 0 or 2 children has nodes count = 2^h - 1, where h is height.Step 2: Calculate height from nodes count
Given nodes = 7, solve 2^h - 1 = 7 -> 2^h = 8 -> h = 3.Final Answer:
3 -> Option AQuick Check:
Full binary tree height for 7 nodes = 3 [OK]
- Confusing height with number of edges
- Using incomplete binary tree formula
- Miscounting nodes or height
Ready to prove it?
Pass the test and this pattern moves unknown
