The height of a binary tree is the number of edges on the longest path from the root node to a leaf node. It measures how tall the tree is.
Click to reveal answer
beginner
How do you calculate the height of an empty binary tree?
The height of an empty binary tree is defined as -1 because there are no nodes or edges.
Click to reveal answer
beginner
What is the height of a tree with only one node (the root)?
The height is 0 because there are no edges from the root to any leaf (the root itself is a leaf).
Click to reveal answer
intermediate
Explain the recursive approach to find the height of a binary tree.
To find the height, recursively find the height of the left and right subtrees, then take the maximum of these two heights and add 1 for the current node.
Click to reveal answer
intermediate
In Rust, what is a common way to represent a binary tree node?
A common way is to use an enum with variants for Empty and NonEmpty nodes, where NonEmpty holds a value and boxed left and right child nodes.
Click to reveal answer
What is the height of a binary tree with only one node?
A1
B2
C-1
D0
✗ Incorrect
A tree with one node has height 0 because there are no edges from root to leaf.
Which of the following best describes the height of a binary tree?
ANumber of leaves in the tree
BNumber of nodes in the tree
CNumber of edges on the longest path from root to leaf
DNumber of children of the root node
✗ Incorrect
Height is the number of edges on the longest path from root to leaf.
What is the height of an empty binary tree?
A0
B-1
C1
DUndefined
✗ Incorrect
By definition, an empty tree has height -1.
In Rust, which keyword is used to allocate nodes on the heap for a binary tree?
ABox
BVec
CRc
DRefCell
✗ Incorrect
Box is used to allocate data on the heap, useful for recursive data structures like trees.
What is the base case in the recursive height calculation of a binary tree?
AWhen the node is null or empty
BWhen the node has two children
CWhen the node is a leaf
DWhen the node has one child
✗ Incorrect
The base case is when the node is empty (null), returning -1.
Describe how to find the height of a binary tree using recursion.
Think about how you break down the problem into smaller parts.
You got /4 concepts.
Explain how a binary tree node can be represented in Rust for height calculation.
Consider how Rust handles recursive data types safely.
You got /3 concepts.
Practice
(1/5)
1. What is the height of a binary tree with only one node (the root)?
easy
A. 1
B. Undefined
C. 2
D. 0
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 D
Quick Check:
Single node tree height = 0 [OK]
Hint: Height counts edges from root to deepest leaf [OK]
Common Mistakes:
Confusing height with number of nodes
Thinking empty tree has height 1
Assuming height starts at 1
2. Which of the following Rust function signatures correctly represents a function to calculate the height of a binary tree node defined as struct Node { left: Option, right: Option }?
easy
A. fn height(node: &Option) -> usize
B. fn height(node: Node) -> usize
C. fn height(node: &Node) -> usize
D. fn height(node: Option<Node>) -> usize
Solution
Step 1: Analyze parameter types
The tree nodes are wrapped in Option to allow nullability and heap allocation.
Step 2: Choose correct reference type
Passing a reference to Option (&Option) avoids moving ownership and allows checking for None.
Final Answer:
fn height(node: &Option) -> usize -> Option A
Quick Check:
Use &Option to handle nullable nodes safely [OK]
Hint: Use &Option to avoid ownership issues [OK]
Common Mistakes:
Passing Node by value moves ownership
Using Option<Node> instead of Option
Using &Node misses None case
3. Given the following Rust code, what is the printed height of the binary tree?
The leaf node has no children, so height = 1 (0 + 1).
Step 2: Trace left_child height
Left_child has left = leaf (height 1), right = None (height 0), so height = 1 + max(1,0) = 2.
Step 3: Trace root height
Root has left = left_child (height 2), right = None (height 0), so height = 1 + max(2,0) = 3.
Final Answer:
3 -> Option C
Quick Check:
Height counts nodes on longest path = 3 [OK]
Hint: Count nodes from root to deepest leaf recursively [OK]
Common Mistakes:
Counting edges instead of nodes
Ignoring None children
Misreading recursive calls
4. Identify the bug in this Rust function to calculate binary tree height and select the correct fix:
fn height(node: Option<Box<Node>>) -> usize {
if node.is_none() {
return 0;
}
let n = node.unwrap();
1 + height(n.left).max(height(n.right))
}
medium
A. Replace unwrap() with expect() to handle errors
B. Change parameter to &Option<Box<Node>> and use references to avoid moving ownership
C. Return 1 instead of 0 when node is None
D. Use Box<Node> instead of Option<Box<Node>> as parameter
Solution
Step 1: Identify ownership issue
Calling unwrap() moves ownership of node, making recursive calls invalid as ownership is lost.
Step 2: Fix by using references
Changing parameter to &Option<Box<Node>> allows borrowing without moving ownership, enabling safe recursion.
Final Answer:
Change parameter to &Option<Box<Node>> and use references to avoid moving ownership -> Option B
Quick Check:
Use references to prevent ownership errors in recursion [OK]
Hint: Use references to avoid moving nodes in recursion [OK]
Common Mistakes:
Using unwrap() moves ownership
Ignoring ownership rules
Changing return values incorrectly
5. You want to find the height of a binary tree but only have access to a vector of edges representing parent-child relationships as tuples (parent, child). Which approach correctly computes the height of the tree in Rust?
hard
A. Build the tree nodes from edges, then recursively compute height from root node
B. Count the number of edges in the vector and return that as height
C. Use the number of unique nodes as the height
D. Return the maximum child value from edges as height
Solution
Step 1: Understand input format
The vector of edges gives parent-child pairs but no direct tree structure.
Step 2: Build tree structure
Construct nodes and link children to parents to form the tree, identifying the root (node with no parent).
Step 3: Compute height recursively
Starting from root node, recursively calculate height by finding max height of left and right subtrees.
Final Answer:
Build the tree nodes from edges, then recursively compute height from root node -> Option A
Quick Check:
Build tree first, then compute height recursively [OK]
Hint: Convert edges to tree nodes before height calculation [OK]
Common Mistakes:
Using edge count as height
Ignoring tree structure
Assuming max child value equals height
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.