BST Find Maximum Element in DSA Rust - Time & Space Complexity
We want to know how long it takes to find the largest value in a Binary Search Tree (BST).
How does the time needed grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
fn find_max(root: &Option>) -> Option {
let mut current = root;
while let Some(node) = current {
if node.right.is_none() {
return Some(node.val);
}
current = &node.right;
}
None
}
This code finds the maximum value by moving right until no more right child exists.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving from one node to its right child in a loop.
- How many times: At most once per level down the right side of the tree.
As the tree grows, the number of steps depends on the height of the rightmost path.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps if tree is skewed right |
| 100 | Up to 100 steps if skewed right |
| 1000 | Up to 1000 steps if skewed right |
Pattern observation: The time grows linearly with the height of the rightmost path, which can be as large as the number of nodes in worst case.
Time Complexity: O(h)
This means the time depends on the height of the tree, moving down the right side until the maximum is found.
[X] Wrong: "Finding the max is always O(1) because we just look at the root."
[OK] Correct: The max is at the rightmost node, not always the root, so we must follow right children which can take time proportional to tree height.
Understanding how tree height affects search time helps you explain and optimize tree operations clearly in interviews.
What if the BST was balanced? How would the time complexity change?
Practice
Solution
Step 1: Understand BST property
In a BST, left child nodes are smaller and right child nodes are larger than the parent node.Step 2: Locate maximum element
The maximum element is the rightmost node because it has no larger right child.Final Answer:
At the rightmost node -> Option CQuick Check:
Maximum = rightmost node [OK]
- Confusing maximum with root node
- Thinking maximum is at leftmost node
- Assuming maximum can be any leaf
Solution
Step 1: Analyze code logic
fn find_max(node: &Option) -> i32 { let mut current = node; while let Some(ref n) = current { if n.right.is_none() { return n.value; } current = &n.right; } unreachable!() } uses a loop to traverse right children until none remain, returning the rightmost node's value.Step 2: Check other options
fn find_max(node: &Option) -> i32 { node.unwrap().value } returns root value only, C returns left child value, D returns constant 0, all incorrect.Final Answer:
Iterative right traversal to rightmost node -> Option AQuick Check:
Traverse right until None = max value [OK]
- Returning root value without traversal
- Accessing left child instead of right
- Returning a fixed value ignoring tree
struct Node { value: i32, left: Option, right: Option }
fn find_max(node: &Option) -> i32 {
let mut current = node;
while let Some(ref n) = current {
if n.right.is_none() {
return n.value;
}
current = &n.right;
}
unreachable!()
}
fn main() {
let tree = Some(Box::new(Node {
value: 10,
left: Some(Box::new(Node { value: 5, left: None, right: None })),
right: Some(Box::new(Node {
value: 15,
left: None,
right: Some(Box::new(Node { value: 20, left: None, right: None }))
}))
}));
println!("{}", find_max(&tree));
}Solution
Step 1: Trace the tree structure
Root is 10, right child 15, right child's right child 20 (rightmost node).Step 2: Follow find_max logic
Function moves right until no right child: stops at node with value 20.Final Answer:
20 -> Option BQuick Check:
Rightmost node value = 20 [OK]
- Returning root value instead of rightmost
- Confusing left and right children
- Not following loop to last right node
fn find_max(node: &Option) -> i32 {
if node.is_none() {
return 0;
}
let mut current = node;
while current.is_some() {
if current.as_ref().unwrap().right.is_none() {
return current.as_ref().unwrap().value;
}
current = ยคt.as_ref().unwrap().left;
}
0
}Solution
Step 1: Analyze traversal direction
Function moves to left child in loop, but max is in right subtree.Step 2: Identify correct traversal
To find max, must traverse right child until None, not left.Final Answer:
It traverses left child instead of right child -> Option DQuick Check:
Max = rightmost node, not left [OK]
- Going left instead of right
- Returning 0 silently for empty tree
- Unwrapping without safe checks
Solution
Step 1: Understand duplicates in BST
In a BST (allowing duplicates typically on right), the maximum value(s) propagate to the rightmost node.Step 2: Correct approach
Traverse right until None; this reaches the rightmost node holding the maximum value, even with duplicates.Final Answer:
Traverse right until None and return the last node's value -> Option AQuick Check:
Rightmost = max even with duplicates [OK]
- Using recursion on left subtree
- Traversing both subtrees unnecessarily
- Returning root value with duplicates
Ready to prove it?
Pass the test and this pattern moves unknown
