Height and depth of trees in Data Structures Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with trees, it is important to understand how the height and depth affect the time it takes to perform operations.
We want to know how the number of steps grows as the tree gets bigger.
Analyze the time complexity of finding the height of a tree using recursion.
function height(node) {
if (node == null) return -1;
let leftHeight = height(node.left);
let rightHeight = height(node.right);
return 1 + Math.max(leftHeight, rightHeight);
}
This code calculates the height by checking the height of left and right subtrees recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls to visit 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 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The number of steps grows directly with the number of nodes.
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 can be found without visiting all nodes."
[OK] Correct: Because the height depends on the longest path, you must check all nodes to be sure.
Understanding how tree height and depth affect time helps you explain and reason about tree operations clearly.
"What if the tree is balanced versus very unbalanced? How would that affect the time complexity of finding height?"
Practice
depth of a node in a tree represent?Solution
Step 1: Understand the definition of depth
Depth is defined as the distance from the root node to the given node, measured in edges.Step 2: Compare with other options
Height measures distance to farthest leaf, not depth. Total nodes and children count are unrelated.Final Answer:
The number of edges from the root to that node -> Option AQuick Check:
Depth = edges from root to node [OK]
- Confusing depth with height
- Thinking depth counts children
- Mixing depth with total nodes
height of a leaf node in a tree?Solution
Step 1: Recall height definition for any node
Height is the number of edges on the longest path from the node down to a leaf.Step 2: Apply to leaf node
A leaf node has no children, so the longest path down is zero edges, making height 0.Final Answer:
Height is 0 because it has no children -> Option BQuick Check:
Leaf height = 0 edges down [OK]
- Assuming height is 1 for leaves
- Confusing height with depth
- Counting siblings as height
A
/ \
B C
/ / \
D E F
/
GWhat is the height of node
C?Solution
Step 1: Identify the subtree rooted at node C
Node C has children E and F; F has child G.Step 2: Find longest path from C down to a leaf
Paths: C->E (1 edge), C->F->G (2 edges). Longest path length is 2 edges.Final Answer:
2 -> Option DQuick Check:
Height of C = longest path down = 2 edges [OK]
- Counting number of children instead of edges
- Confusing height with depth
- Ignoring deeper descendants
Solution
Step 1: Recall definition of depth for root
Depth is edges from root to node; root is at distance zero from itself.Step 2: Identify error in student's statement
Student incorrectly assigns depth 1 to root; correct depth is 0.Final Answer:
Depth of root is always 0, not 1 -> Option CQuick Check:
Root depth = 0 edges [OK]
- Assigning depth 1 to root
- Confusing depth with height
- Thinking depth depends on children
Solution
Step 1: Understand height of leaf nodes
Leaf nodes have height 0 because they have no children below.Step 2: Apply to leaf at depth 4
Since the node at depth 3 has height 1, its child at depth 4 must be a leaf with height 0.Final Answer:
0 -> Option AQuick Check:
Leaf node height = 0 [OK]
- Assuming height equals depth
- Thinking height increases with depth
- Confusing height with number of siblings
