Concept Flow - BST Find Minimum Element
Start at root node
Check if left child exists?
No→Current node is minimum
Yes
Move to left child
Repeat check
Start from the root and keep moving left until no left child exists; that node is the minimum.
Node findMin(Node root) {
Node current = root;
while (current.left != null) {
current = current.left;
}
return current;
}| Step | Operation | Current Node | Left Child Exists? | Pointer Changes | Visual State |
|---|---|---|---|---|---|
| 1 | Start at root | 50 | Yes | current = 50 | 50 / \ 30 70 |
| 2 | Move left | 30 | Yes | current = 30 | 50 / \ 30 70 / 20 |
| 3 | Move left | 20 | No | current = 20 | 50 / \ 30 70 / 20 |
| 4 | Return current | 20 | No | Return node 20 | Minimum found: 20 |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| current | root (50) | 50 | 30 | 20 | 20 |
BST Find Minimum Element: - Start at root node - While left child exists, move left - When no left child, current node is minimum - Return current node - Works because BST left subtree has smaller values
root?class Node {
int data;
Node left, right;
Node(int d) { data = d; left = right = null; }
}
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;
}
System.out.println(current.data);public int findMin(Node root) {
if(root == null) return -1;
while(root.right != null) {
root = root.right;
}
return root.data;
}public int findMinUnique(Node root) {
Set<Integer> seen = new HashSet<>();
int min = Integer.MAX_VALUE;
while(root != null) {
if(!seen.contains(root.data)) {
min = Math.min(min, root.data);
seen.add(root.data);
}
root = root.left;
}
return min;
}Pass the test and this pattern moves unknown