Tree Terminology Root Leaf Height Depth Level in DSA Rust - Time & Space Complexity
We want to understand how the time to visit parts of a tree grows as the tree gets bigger.
How does the number of steps change when we look at nodes from root to leaves?
Analyze the time complexity of the following code snippet.
struct Node {
value: i32,
children: Vec<Node>,
}
fn height(node: &Node) -> usize {
if node.children.is_empty() {
0
} else {
1 + node.children.iter().map(|child| height(child)).max().unwrap_or(0)
}
}
This code finds the height of a tree by checking all children recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls to visit each child node.
- How many times: Once for every node in the tree.
As the tree grows with more nodes, the function visits each node once.
| 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 a straight line with the number of nodes.
[X] Wrong: "The height function only looks at the longest path, so it runs in constant time."
[OK] Correct: Even though height depends on the longest path, the function must visit every node to be sure, so it takes time proportional to all nodes.
Understanding how tree operations scale helps you explain your code clearly and shows you know how data grows in real problems.
"What if the tree was a linked list (each node has only one child)? How would the time complexity change?"
Practice
root?Solution
Step 1: Understand the root node concept
The root node is the starting point of a tree and has no parent node above it.Step 2: Differentiate root from other nodes
Leaf nodes have no children, and other nodes may have parents. Only the root has no parent.Final Answer:
The topmost node with no parent -> Option AQuick Check:
Root = top node with no parent [OK]
- Confusing root with leaf nodes
- Thinking root must have children
- Assuming root is the deepest node
Solution
Step 1: Identify leaf node properties
A leaf node has no children, so both left and right must be None.Step 2: Check each option for correct leaf definition
let leaf = Node { value: 5, left: None, right: None }; sets left and right to None, correctly defining a leaf node.Final Answer:
let leaf = Node { value: 5, left: None, right: None }; -> Option CQuick Check:
Leaf node has no children (left and right None) [OK]
- Setting left or right to Some(Box::new(Node)) for leaf
- Confusing struct definition with instance creation
- Missing Option wrapping for children
struct Node {
value: i32,
left: Option,
right: Option,
}
let root = Node {
value: 1,
left: Some(Box::new(Node {
value: 2,
left: Some(Box::new(Node {
value: 4,
left: None,
right: None,
})),
right: None,
})),
right: Some(Box::new(Node {
value: 3,
left: None,
right: None,
})),
};Solution
Step 1: Understand depth definition
Depth is the number of edges from the root to the node.Step 2: Trace path from root to node 4
Path: root(1) -> left(2) -> left(4). Number of edges = 2.Final Answer:
2 -> Option AQuick Check:
Depth of node 4 = 2 edges from root [OK]
- Counting nodes instead of edges
- Confusing depth with height
- Miscounting path steps
fn height(node: &Option) -> i32 {
if let Some(n) = node {
let left_height = height(&n.left);
let right_height = height(&n.right);
left_height + right_height + 1
} else {
0
}
}Solution
Step 1: Understand height calculation logic
Height is the longest path from node to leaf, so use max(left, right) + 1.Step 2: Identify bug in code
Code adds left_height + right_height + 1, which sums both sides incorrectly.Final Answer:
It should return the maximum of left and right heights, not their sum -> Option BQuick Check:
Height = max(left, right) + 1 [OK]
- Summing left and right heights instead of max
- Confusing height with node count
- Incorrect base case return value
Solution
Step 1: Understand level and depth relationship
Level and depth both count edges from root; level 0 means root, so level 3 means depth 3.Step 2: Determine height of leaf node subtree
A node with no children is a leaf, so its height is 0.Final Answer:
Depth = 3, Height = 0 -> Option DQuick Check:
Level = Depth = 3; Leaf height = 0 [OK]
- Confusing height with depth
- Assuming leaf height is 1
- Mixing level numbering starting at 1
Ready to prove it?
Pass the test and this pattern moves unknown
