What if you could find the smallest item instantly without searching everything?
Why BST Find Minimum Element in DSA C++?
Imagine you have a messy pile of books with no order. You want to find the oldest book, but you have to check every single one by hand.
Looking through every book one by one takes a lot of time and effort. You might miss some or get tired, making mistakes easy.
A Binary Search Tree (BST) keeps books sorted so the oldest book is always on the leftmost side. You just follow the left path to find it quickly and easily.
int findMin(int arr[], int size) {
int min = arr[0];
for(int i = 1; i < size; i++) {
if(arr[i] < min) min = arr[i];
}
return min;
}Node* findMin(Node* root) {
while(root->left != nullptr) {
root = root->left;
}
return root;
}This lets you find the smallest value instantly without checking every item, saving time and effort.
In a phone contact list sorted by name, finding the first contact alphabetically is like finding the minimum in a BST.
Manual search is slow and error-prone.
BST structure keeps data sorted for quick minimum search.
Following left children leads directly to the minimum element.
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 left until no more left child exists.Final Answer:
At the leftmost node -> Option BQuick Check:
Minimum = leftmost node [OK]
- Thinking minimum is at root
- Confusing leftmost with rightmost
- Assuming minimum can be any leaf
root?Solution
Step 1: Check traversal direction
Minimum is found by moving left until no left child exists.Step 2: Verify code correctness
while(root->left != nullptr) root = root->left; return root->data; loops left correctly and returns the leftmost node's data.Final Answer:
while(root->left != nullptr) root = root->left; return root->data; -> Option AQuick Check:
Traverse left to find minimum [OK]
- Looping right child instead of left
- Returning root data without traversal
- Not checking for nullptr before access
struct Node {
int data;
Node* left;
Node* right;
};
Node* root = new Node{10, nullptr, nullptr};
root->left = new Node{5, nullptr, nullptr};
root->right = new Node{15, nullptr, nullptr};
root->left->left = new Node{2, nullptr, nullptr};
Node* temp = root;
while(temp->left != nullptr) {
temp = temp->left;
}
std::cout << temp->data << std::endl;Solution
Step 1: Trace the tree structure
Root is 10, left child 5, left-left child 2, right child 15.Step 2: Follow the loop to find minimum
Loop moves from 10 to 5, then to 2, which has no left child.Final Answer:
2 -> Option CQuick Check:
Leftmost node data = 2 [OK]
- Stopping at root or first left child
- Confusing right child as minimum
- Not tracing loop fully
int findMin(Node* root) {
if(root == nullptr) return -1;
while(root->right != nullptr) {
root = root->right;
}
return root->data;
}Solution
Step 1: Analyze loop direction
Minimum element is found by moving left, not right.Step 2: Identify incorrect traversal
The code loops on root->right, which finds maximum, not minimum.Final Answer:
Loops on right child instead of left child -> Option AQuick Check:
Minimum requires left traversal [OK]
- Using right child loop for minimum
- Ignoring nullptr checks
- Confusing min and max logic
Solution
Step 1: Understand edge case
If no left child exists, root itself is minimum.Step 2: Modify search logic
Traverse left until nullptr; if none found, root is minimum.Final Answer:
Traverse left child until nullptr; if none, root is minimum -> Option DQuick Check:
Minimum = leftmost or root if no left child [OK]
- Assuming minimum always has left child
- Traversing right child for minimum
- Not handling single-child nodes
Ready to prove it?
Pass the test and this pattern moves unknown
