Bird
Raised Fist0
DSA Cprogramming~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 output in BST
What is the output of the following C code that finds the minimum element in a BST?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* left;
    struct Node* right;
} Node;

Node* newNode(int data) {
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}

Node* insert(Node* root, int data) {
    if (root == NULL) return newNode(data);
    if (data < root->data) root->left = insert(root->left, data);
    else root->right = insert(root->right, data);
    return root;
}

int findMin(Node* root) {
    Node* current = root;
    while (current && current->left != NULL) {
        current = current->left;
    }
    return current->data;
}

int main() {
    Node* root = NULL;
    root = insert(root, 20);
    root = insert(root, 10);
    root = insert(root, 30);
    root = insert(root, 5);
    root = insert(root, 15);
    printf("%d\n", findMin(root));
    return 0;
}
A5
B10
C20
D15
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
❓ Predict Output
intermediate
2:00remaining
Find minimum element with single node BST
What will be printed when finding the minimum element in a BST with only one node?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* left;
    struct Node* right;
} Node;

Node* newNode(int data) {
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}

int findMin(Node* root) {
    Node* current = root;
    while (current && current->left != NULL) {
        current = current->left;
    }
    return current->data;
}

int main() {
    Node* root = newNode(42);
    printf("%d\n", findMin(root));
    return 0;
}
ASegmentation fault
B0
C42
DUndefined behavior
Attempts:
2 left
💡 Hint
With only one node, that node is the minimum.
❓ Predict Output
advanced
2:00remaining
Find minimum element after inserting multiple nodes
What is the output of the following code after inserting nodes in this order: 50, 30, 70, 20, 40, 60, 80?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* left;
    struct Node* right;
} Node;

Node* newNode(int data) {
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}

Node* insert(Node* root, int data) {
    if (root == NULL) return newNode(data);
    if (data < root->data) root->left = insert(root->left, data);
    else root->right = insert(root->right, data);
    return root;
}

int findMin(Node* root) {
    Node* current = root;
    while (current && current->left != NULL) {
        current = current->left;
    }
    return current->data;
}

int main() {
    Node* root = NULL;
    int values[] = {50, 30, 70, 20, 40, 60, 80};
    for (int i = 0; i < 7; i++) {
        root = insert(root, values[i]);
    }
    printf("%d\n", findMin(root));
    return 0;
}
A20
B30
C40
D50
Attempts:
2 left
💡 Hint
The minimum is the smallest value inserted, which is the leftmost node.
❓ Predict Output
advanced
2:00remaining
Find minimum element in an empty BST
What happens when you call findMin on an empty BST (root is NULL)?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* left;
    struct Node* right;
} Node;

int findMin(Node* root) {
    Node* current = root;
    while (current && current->left != NULL) {
        current = current->left;
    }
    return current->data;
}

int main() {
    Node* root = NULL;
    printf("%d\n", findMin(root));
    return 0;
}
A0
BCompilation error
C-1
DSegmentation fault
Attempts:
2 left
💡 Hint
Accessing data from a NULL pointer causes a runtime error.
🧠 Conceptual
expert
2:00remaining
Why does findMin traverse left children in BST?
In a Binary Search Tree (BST), why does the findMin function traverse only the left children to find the minimum element?
ABecause the minimum element is always the root node in a BST.
BBecause in BST, all left children have smaller values than their parent nodes, so the leftmost node is the minimum.
CBecause the right children always contain larger values, so they are ignored to find the minimum.
DBecause traversing right children would find the maximum, not the minimum.
Attempts:
2 left
💡 Hint
Think about the BST property for left and right children values.

Practice

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

Solution

  1. Step 1: Understand BST property

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

    Because smaller values go left, the minimum is at the leftmost node.
  3. Final Answer:

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

    Minimum element = leftmost node [OK]
Hint: Minimum is always the farthest left 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?
easy
A. while(root->left != NULL) root = root->right; return root->data;
B. while(root->left != NULL) root = root->left; return root->data;
C. if(root == NULL) return INT_MAX; return root->data;
D. while(root->right != NULL) root = root->right; return root->data;

Solution

  1. Step 1: Check loop condition for minimum

    Minimum is found by moving left while left child exists.
  2. Step 2: Verify code correctness

    while(root->left != NULL) root = root->left; return root->data; moves left until no left child, then returns data correctly.
  3. Final Answer:

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

    Traverse left to find minimum [OK]
Hint: Traverse left child until NULL to find minimum [OK]
Common Mistakes:
  • Traversing right child instead of left
  • Returning root data without traversal
  • Incorrect loop condition
3. What will be the output of the following C code snippet for a BST?
struct Node {
  int data;
  struct Node* left;
  struct Node* right;
};

struct Node* findMin(struct Node* root) {
  while(root->left != NULL) {
    root = root->left;
  }
  return root;
}

// Assume BST is:
//       10
//      /  \
//     5    15
//    / 
//   2

int main() {
  struct Node n2 = {2, NULL, NULL};
  struct Node n5 = {5, &n2, NULL};
  struct Node n15 = {15, NULL, NULL};
  struct Node n10 = {10, &n5, &n15};
  struct Node* minNode = findMin(&n10);
  printf("%d", minNode->data);
  return 0;
}
medium
A. 10
B. 5
C. 2
D. 15

Solution

  1. Step 1: Trace findMin function

    Function moves left from root 10 to 5, then to 2, stops as 2 has no left child.
  2. Step 2: Identify returned node data

    Returned node is 2, so printf prints 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Leftmost node data = 2 [OK]
Hint: Output is leftmost node's data in BST [OK]
Common Mistakes:
  • Confusing leftmost with root
  • Assuming right child is minimum
  • Not following pointer traversal
4. Identify the error in the following C function that tries to find the minimum element in a BST:
struct Node* findMin(struct Node* root) {
  while(root->left == NULL) {
    root = root->left;
  }
  return root;
}
medium
A. Loop condition should be root->left != NULL
B. Function should return root->right
C. Missing check for root == NULL before loop
D. Should use recursion instead of loop

Solution

  1. Step 1: Analyze loop condition

    Loop runs while root->left == NULL, which means it stops if left child exists, so it never moves left.
  2. Step 2: Correct loop condition

    It should run while root->left != NULL to move left until no left child.
  3. Final Answer:

    Loop condition should be root->left != NULL -> Option A
  4. Quick Check:

    Traverse left while left child exists [OK]
Hint: Loop must continue while left child exists [OK]
Common Mistakes:
  • Using == NULL instead of != NULL in loop
  • Ignoring NULL root check
  • Confusing left and right pointers
5. Given a BST where some nodes have only right children, how would you modify the findMin function to safely find the minimum element without causing a runtime error if the tree is empty?
hard
A. Traverse right child instead of left child
B. Return root->data immediately without traversal
C. Use recursion without base case
D. Add a check at start: if(root == NULL) return NULL; then traverse left

Solution

  1. Step 1: Handle empty tree case

    Check if root is NULL at start to avoid dereferencing NULL pointer.
  2. Step 2: Traverse left children safely

    If root is not NULL, move left while left child exists to find minimum.
  3. Final Answer:

    Add a check at start: if(root == NULL) return NULL; then traverse left -> Option D
  4. Quick Check:

    Check NULL root before traversal [OK]
Hint: Always check for NULL root before traversal [OK]
Common Mistakes:
  • Not checking for empty tree causing crash
  • Traversing right child for minimum
  • Missing base case in recursion
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