Understanding the Problem
We are given a Binary Search Tree (BST), and we are asked to find the minimum value present in it. A Binary Search Tree is a binary tree where for each node:
- All values in the left subtree are less than the node.
- All values in the right subtree are greater than the node.
The minimum value in a BST will always be the leftmost node because smaller values are always inserted to the left. Let's understand this with a clear example and then walk through the solution step by step.
Step-by-Step Solution with Example
Step 1: Look at the Root
Start with the root node of the BST. This is where our search for the minimum value begins. Let's consider the following example tree:
10
/ 5 15
/
2
The root node here is 10.
Step 2: Move to the Left Child
According to BST rules, the left child will always have a smaller value. So from 10, we move left to 5.
Step 3: Keep Going Left
From 5, we again move left to 2, because we are looking for the smallest value. If 2 had a left child, we would continue left. But 2 has no left child.
Step 4: Stop at the Leftmost Node
Since 2 has no further left children, it is the minimum value in this BST. This logic applies to all BSTs — the leftmost node is always the smallest.
Edge Cases
Case 1: Left-skewed Tree
20
/
10
/
5
/
3
In this case, we keep moving left from 20 → 10 → 5 → 3. The node 3 has no left child, so it is the minimum value.
Case 2: Single-node Tree
42
There are no left or right children. The root itself is the minimum value.
Case 3: Empty Tree
If the BST is empty (i.e., root is null), there is no minimum value. Your function should handle this case safely by either:
- Returning
null or None,
- Throwing an appropriate exception, or
- Returning a message that the tree is empty.
Finally
Finding the minimum in a BST is a very intuitive process — just keep going left until you can't go any further. This works because of the BST property that all smaller elements lie to the left. Make sure your code handles edge cases like empty trees and single-node trees, especially in interviews or production-level code. A safe, defensive approach leads to more reliable software.