Bird
Raised Fist0
DSA C++programming~20 mins

BST Find Minimum Element in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Minimum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
A7
B2
C5
D10
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
❓ Predict Output
intermediate
2: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;
}
ASegmentation fault
B0
C42
Dnullptr
Attempts:
2 left
💡 Hint
If there is only one node, it is the minimum.
❓ Predict Output
advanced
2: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;
}
ASegmentation fault
B0
CCompilation error
D-1
Attempts:
2 left
💡 Hint
Accessing members of a nullptr causes a runtime error.
❓ Predict Output
advanced
2: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;
}
A5
B10
C15
D20
Attempts:
2 left
💡 Hint
The recursion goes left until no left child exists.
🧠 Conceptual
expert
2: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?
ABecause BSTs store values in random order, so the minimum is always the leftmost by chance.
BBecause the right subtree always contains larger values, so the minimum must be on the rightmost node.
CBecause the root node always contains the minimum value in a BST.
DBecause all left children have smaller values than their parents, so going left leads to the smallest value.
Attempts:
2 left
💡 Hint
Think about the BST property for left and right children.

Practice

(1/5)
1. In a Binary Search Tree (BST), where is the minimum element always located?
easy
A. At any leaf node
B. At the leftmost node
C. At the root node
D. At the rightmost node

Solution

  1. Step 1: Understand BST property

    In a BST, left child nodes have smaller values than their parents.
  2. Step 2: Locate minimum element

    The smallest value is found by going left until no more left child exists.
  3. Final Answer:

    At the leftmost node -> Option B
  4. Quick Check:

    Minimum = leftmost node [OK]
Hint: Minimum is always the leftmost node in BST [OK]
Common Mistakes:
  • Thinking minimum is at root
  • Confusing leftmost with rightmost
  • Assuming minimum can be any leaf
2. Which of the following C++ code snippets correctly finds the minimum element in a BST node pointer root?
easy
A. while(root->left != nullptr) root = root->left; return root->data;
B. while(root->right != nullptr) root = root->right; return root->data;
C. if(root == nullptr) return INT_MAX; return root->data;
D. return root->data;

Solution

  1. Step 1: Check traversal direction

    Minimum is found by moving left until no left child exists.
  2. 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.
  3. Final Answer:

    while(root->left != nullptr) root = root->left; return root->data; -> Option A
  4. Quick Check:

    Traverse left to find minimum [OK]
Hint: Loop left child until null to find minimum [OK]
Common Mistakes:
  • Looping right child instead of left
  • Returning root data without traversal
  • Not checking for nullptr before access
3. What is the output of the following C++ code snippet?
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;
medium
A. 10
B. 5
C. 2
D. 15

Solution

  1. Step 1: Trace the tree structure

    Root is 10, left child 5, left-left child 2, right child 15.
  2. Step 2: Follow the loop to find minimum

    Loop moves from 10 to 5, then to 2, which has no left child.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Leftmost node data = 2 [OK]
Hint: Follow left pointers until null to find minimum [OK]
Common Mistakes:
  • Stopping at root or first left child
  • Confusing right child as minimum
  • Not tracing loop fully
4. Identify the error in the following C++ function to find the minimum element in a BST:
int findMin(Node* root) {
  if(root == nullptr) return -1;
  while(root->right != nullptr) {
    root = root->right;
  }
  return root->data;
}
medium
A. Loops on right child instead of left child
B. Returns -1 when root is nullptr
C. Returns root->data without checking nullptr
D. Function return type should be Node*

Solution

  1. Step 1: Analyze loop direction

    Minimum element is found by moving left, not right.
  2. Step 2: Identify incorrect traversal

    The code loops on root->right, which finds maximum, not minimum.
  3. Final Answer:

    Loops on right child instead of left child -> Option A
  4. Quick Check:

    Minimum requires left traversal [OK]
Hint: Minimum is leftmost, not rightmost [OK]
Common Mistakes:
  • Using right child loop for minimum
  • Ignoring nullptr checks
  • Confusing min and max logic
5. Given a BST where some nodes have only right children, how would you modify the standard minimum element search to handle this edge case correctly?
hard
A. Return root data immediately if left child is nullptr
B. Traverse right child until nullptr to find minimum
C. Use recursion to check both left and right children
D. Traverse left child until nullptr; if none, root is minimum

Solution

  1. Step 1: Understand edge case

    If no left child exists, root itself is minimum.
  2. Step 2: Modify search logic

    Traverse left until nullptr; if none found, root is minimum.
  3. Final Answer:

    Traverse left child until nullptr; if none, root is minimum -> Option D
  4. Quick Check:

    Minimum = leftmost or root if no left child [OK]
Hint: If no left child, root is minimum [OK]
Common Mistakes:
  • Assuming minimum always has left child
  • Traversing right child for minimum
  • Not handling single-child nodes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard