Tree Terminology Root Leaf Height Depth Level in DSA C - Time & Space Complexity
We want to understand how the time to explore a tree grows as the tree gets bigger.
How does the number of steps change when we look at nodes like root, leaves, or levels?
Analyze the time complexity of the following code snippet.
// Simple recursive function to find height of a binary tree
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;
}
This code finds the height of a 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 height.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: Operations grow directly with the number of nodes.
Time Complexity: O(n)
This means the time to find height grows linearly with the number of nodes in the tree.
[X] Wrong: "The height function only checks the root and leaves, so it runs in constant time."
[OK] Correct: The function must visit every node to find the longest path, not just root or leaves.
Understanding how tree operations scale helps you explain and write efficient tree algorithms confidently.
"What if we changed the tree to be very unbalanced (like a linked list)? How would the time complexity change?"
Practice
root?Solution
Step 1: Understand the root node definition
The root node is the starting point of the tree and has no parent node above it.Step 2: Identify the correct node type
Nodes with no children are called leaves, not root. The root is unique and at the top.Final Answer:
The topmost node with no parent -> Option BQuick Check:
Root = topmost node with no parent [OK]
- Confusing root with leaf nodes
- Thinking root has children but no parent
- Assuming root is the deepest node
Solution
Step 1: Define a leaf node
A leaf node is a node that does not have any children nodes connected below it.Step 2: Check options against definition
Only the option "A node with no children" matches the leaf node definition.Final Answer:
A node with no children -> Option AQuick Check:
Leaf = node with no children [OK]
- Thinking leaf nodes can have children
- Confusing leaf with root node
- Assuming leaf nodes must have a parent
A
/ \
B C
/ / \
D E F
What is the height of node C?Solution
Step 1: Understand height of a node
Height is the number of edges on the longest path from the node down to a leaf.Step 2: Calculate height of node C
Node C has children E and F, both leaves (height 0). So height of C = 1 + max(0,0) = 1.Step 3: Verify options
1 says 1, but height counts edges down, so height is 1. 2 says 2, which is incorrect. Actually, height is 1.Final Answer:
1 -> Option DQuick Check:
Height of C = 1 edge to leaf [OK]
- Counting nodes instead of edges for height
- Confusing height with depth
- Ignoring that leaves have height 0
int depth(Node* root, Node* target) {
if (root == NULL) return -1;
if (root == target) return 0;
int d = depth(root->left, target);
if (d >= 0) return d + 1;
d = depth(root->right, target);
if (d >= 0) return d + 1;
return -1;
}Solution
Step 1: Analyze base cases
If root is NULL, returns -1 (not found). If root is target, returns 0 (depth zero at root).Step 2: Check recursive calls
Recursively checks left subtree; if found, adds 1. Else checks right subtree similarly. Returns -1 if not found.Step 3: Confirm correctness
This correctly calculates depth as number of edges from root to target, starting at 0 for root.Final Answer:
Function correctly calculates depth -> Option CQuick Check:
Depth function logic is correct [OK]
- Returning wrong base case values
- Not checking both left and right subtrees
- Confusing depth with height
level of a node is defined as its depth plus 1. Given the tree below: R
/ \
A B
/ \
C D
\
E
What is the level of node E?Solution
Step 1: Find depth of node E
Depth is number of edges from root R to E. Path: R -> B -> D -> E has 3 edges, so depth = 3.Step 2: Calculate level from depth
Level = depth + 1 = 3 + 1 = 4.Final Answer:
4 -> Option AQuick Check:
Level = depth + 1 = 4 [OK]
- Counting nodes instead of edges for depth
- Forgetting to add 1 for level
- Confusing level with height
Ready to prove it?
Pass the test and this pattern moves unknown
