Challenge - 5 Problems
BST Minimum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find minimum element in a BST
What is the output of the following C++ code that finds the minimum element in a Binary Search Tree (BST)?
DSA C++
#include <iostream> struct Node { int data; Node* left; Node* right; }; int findMin(Node* root) { while (root->left != nullptr) { root = root->left; } return root->data; } int main() { Node n1 = {10, nullptr, nullptr}; Node n2 = {5, nullptr, nullptr}; Node n3 = {15, nullptr, nullptr}; Node n4 = {2, nullptr, nullptr}; Node n5 = {7, nullptr, nullptr}; n1.left = &n2; n1.right = &n3; n2.left = &n4; n2.right = &n5; int minVal = findMin(&n1); std::cout << minVal << std::endl; return 0; }
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
✗ Incorrect
The function moves left until it finds the leftmost node, which has the smallest value. Here, node with value 2 is the leftmost.
❓ Predict Output
intermediate2:00remaining
Minimum element in BST with single node
What will be the output of the following code that finds the minimum element in a BST with only one node?
DSA C++
#include <iostream> struct Node { int data; Node* left; Node* right; }; int findMin(Node* root) { while (root->left != nullptr) { root = root->left; } return root->data; } int main() { Node root = {42, nullptr, nullptr}; int minVal = findMin(&root); std::cout << minVal << std::endl; return 0; }
Attempts:
2 left
💡 Hint
If there is only one node, it is the minimum.
✗ Incorrect
Since the root has no left child, the loop does not run and the root's data (42) is returned.
❓ Predict Output
advanced2:00remaining
Find minimum element in BST with null root
What error or output will the following code produce when trying to find the minimum element in an empty BST (root is nullptr)?
DSA C++
#include <iostream> struct Node { int data; Node* left; Node* right; }; int findMin(Node* root) { while (root->left != nullptr) { root = root->left; } return root->data; } int main() { Node* root = nullptr; int minVal = findMin(root); std::cout << minVal << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Accessing members of a nullptr causes a runtime error.
✗ Incorrect
The function tries to access root->left when root is nullptr, causing a segmentation fault.
❓ Predict Output
advanced2:00remaining
Find minimum element using recursion
What is the output of the following recursive function to find the minimum element in a BST?
DSA C++
#include <iostream> struct Node { int data; Node* left; Node* right; }; int findMin(Node* root) { if (root == nullptr) return -1; if (root->left == nullptr) return root->data; return findMin(root->left); } int main() { Node n1 = {20, nullptr, nullptr}; Node n2 = {10, nullptr, nullptr}; Node n3 = {30, nullptr, nullptr}; Node n4 = {5, nullptr, nullptr}; Node n5 = {15, nullptr, nullptr}; n1.left = &n2; n1.right = &n3; n2.left = &n4; n2.right = &n5; int minVal = findMin(&n1); std::cout << minVal << std::endl; return 0; }
Attempts:
2 left
💡 Hint
The recursion goes left until no left child exists.
✗ Incorrect
The recursive function returns the data of the leftmost node, which is 5 here.
🧠 Conceptual
expert2:00remaining
Why is the leftmost node the minimum in a BST?
In a Binary Search Tree (BST), why is the minimum element always found at the leftmost node?
Attempts:
2 left
💡 Hint
Think about the BST property for left and right children.
✗ Incorrect
By definition, in a BST, left children are smaller than their parent nodes, so the smallest value is found by going left until no more left child exists.