BST Find Minimum Element in DSA C# - Time & Space Complexity
We want to know how long it takes to find the smallest value in a Binary Search Tree (BST).
How does the time grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
public int FindMin(Node root) {
Node current = root;
while (current.Left != null) {
current = current.Left;
}
return current.Value;
}
This code finds the minimum value by going left until no more left child exists.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving from one node to its left child.
- How many times: At most once per level down the left side of the tree.
As the tree grows taller, the number of steps grows with the height of the tree.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 4 steps (if tree is balanced) |
| 100 | Up to 7 steps |
| 1000 | Up to 10 steps |
Pattern observation: The steps grow slowly with the height, not the total number of nodes.
Time Complexity: O(h)
This means the time depends on the height of the tree, which is how many levels it has.
[X] Wrong: "Finding the minimum always takes time proportional to the total number of nodes (n)."
[OK] Correct: We only move down the left side, so time depends on the tree's height, not total nodes.
Understanding how tree height affects search time helps you explain why balanced trees are faster and shows your grasp of data structure efficiency.
"What if the tree is perfectly balanced versus completely skewed? How would the time complexity change?"
Practice
Solution
Step 1: Understand BST property
In a BST, left child nodes have smaller values than their parents.Step 2: Locate minimum element
The smallest value is found by going as far left as possible from the root.Final Answer:
At the leftmost node -> Option CQuick Check:
Minimum element = leftmost node [OK]
- Thinking minimum is at root always
- Confusing leftmost with rightmost node
- Assuming minimum can be any leaf
root?Solution
Step 1: Check loop condition for minimum
Minimum is found by moving left while left child exists.Step 2: Validate code correctness
while (root.Left != null) { root = root.Left; } return root.Value; moves left until null, then returns last node's value correctly.Final Answer:
while (root.Left != null) { root = root.Left; } return root.Value; -> Option AQuick Check:
Traverse left until null, then return value [OK]
- Traversing right child instead of left
- Returning root value without traversal
- Looping until root is null causing error
class Node {
public int Value;
public Node Left, Right;
public Node(int val) { Value = val; }
}
Node root = new Node(10);
root.Left = new Node(5);
root.Right = new Node(15);
root.Left.Left = new Node(2);
root.Left.Right = new Node(7);
Node current = root;
while (current.Left != null) {
current = current.Left;
}
Console.WriteLine(current.Value);Solution
Step 1: Trace the tree structure
Root is 10, left child 5, left-left child 2, left-right child 7.Step 2: Follow the loop to find minimum
Loop moves from 10 to 5, then to 2 (leftmost node). 2 has no left child, loop stops.Final Answer:
2 -> Option BQuick Check:
Leftmost node value = 2 [OK]
- Printing root value instead of leftmost
- Confusing left-right child as minimum
- Stopping loop too early
int FindMin(Node root) {
while (root != null) {
root = root.Left;
}
return root.Value;
}Solution
Step 1: Analyze loop condition
Loop runs while root is not null, but root becomes null after last left move.Step 2: Identify cause of error
After loop, root is null, so accessing root.Value causes error. Condition should be root.Left != null to stop at last node.Final Answer:
The loop condition should be root.Left != null, not root != null -> Option DQuick Check:
Stop before null to access valid node [OK]
- Looping until root is null causing null reference
- Returning value after root becomes null
- Checking right child instead of left
class Node {
public int Value;
public Node Left, Right;
public Node(int val) { Value = val; }
}
// Duplicates go to right subtree
Which code snippet correctly finds the minimum element?Solution
Step 1: Understand duplicate placement rule
Duplicates go to right subtree, so minimum is still in leftmost path.Step 2: Confirm traversal for minimum
Traverse left child nodes until no more left child exists to find minimum.Final Answer:
while (root.Left != null) { root = root.Left; } return root.Value; -> Option AQuick Check:
Minimum is leftmost node even with duplicates on right [OK]
- Checking right subtree for minimum
- Assuming root is minimum without traversal
- Adding extra conditions unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
