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 A
Quick Check:
Depth of node 4 = 2 edges from root [OK]
Hint: Count edges from root to node for depth [OK]
Common Mistakes:
Counting nodes instead of edges
Confusing depth with height
Miscounting path steps
4. The following Rust code attempts to calculate the height of a binary tree node but has a bug. What is the error?
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
}
}
medium
A. The function should add 2 instead of 1
B. It should return the maximum of left and right heights, not their sum
C. The function should take &Node, not &Option
D. It should return 0 for Some nodes, not null nodes
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 B
Quick Check:
Height = max(left, right) + 1 [OK]
Hint: Height uses max of children heights, not sum [OK]
Common Mistakes:
Summing left and right heights instead of max
Confusing height with node count
Incorrect base case return value
5. You have a tree where the root is at level 0. If a node is at level 3, what is its depth and what is the height of the subtree rooted at this node if it has no children?
hard
A. Depth = 0, Height = 0
B. Depth = 0, Height = 3
C. Depth = 3, Height = 3
D. Depth = 3, Height = 0
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 D
Quick Check:
Level = Depth = 3; Leaf height = 0 [OK]
Hint: Leaf node height is 0; level equals depth from root [OK]
Common Mistakes:
Confusing height with depth
Assuming leaf height is 1
Mixing level numbering starting at 1
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.