Bird
Raised Fist0
DSA Javaprogramming~20 mins

BST Find Minimum Element in DSA Java - 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 Java code that finds the minimum element in a Binary Search Tree (BST)?
DSA Java
class Node {
    int data;
    Node left, right;
    Node(int data) {
        this.data = data;
        left = right = null;
    }
}

public class BST {
    Node root;

    int findMin(Node node) {
        if (node == null) return -1;
        while (node.left != null) {
            node = node.left;
        }
        return node.data;
    }

    public static void main(String[] args) {
        BST tree = new BST();
        tree.root = new Node(10);
        tree.root.left = new Node(5);
        tree.root.right = new Node(15);
        tree.root.left.left = new Node(2);
        tree.root.left.right = new Node(7);
        System.out.println(tree.findMin(tree.root));
    }
}
A2
B5
C7
D10
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
❓ Predict Output
intermediate
2:00remaining
Find minimum element when BST is empty
What is the output of the following Java code when the BST is empty?
DSA Java
class Node {
    int data;
    Node left, right;
    Node(int data) {
        this.data = data;
        left = right = null;
    }
}

public class BST {
    Node root;

    int findMin(Node node) {
        if (node == null) return -1;
        while (node.left != null) {
            node = node.left;
        }
        return node.data;
    }

    public static void main(String[] args) {
        BST tree = new BST();
        System.out.println(tree.findMin(tree.root));
    }
}
A0
B-1
CNullPointerException
DThrows runtime error
Attempts:
2 left
💡 Hint
Check the base case when the node is null.
🔧 Debug
advanced
2:00remaining
Identify the error in BST minimum element code
What error will the following Java code produce when trying to find the minimum element in a BST?
DSA Java
class Node {
    int data;
    Node left, right;
    Node(int data) {
        this.data = data;
        left = right = null;
    }
}

public class BST {
    Node root;

    int findMin(Node node) {
        while (node.left != null) {
            node = node.left;
        }
        return node.data;
    }

    public static void main(String[] args) {
        BST tree = new BST();
        System.out.println(tree.findMin(tree.root));
    }
}
ANullPointerException
BReturns -1
CCompilation error
DReturns 0
Attempts:
2 left
💡 Hint
What happens if node is null and you access node.left?
🧠 Conceptual
advanced
2:00remaining
Why does findMin in BST use left child traversal?
Why does the method to find the minimum element in a Binary Search Tree (BST) traverse only the left children?
ABecause BST nodes have equal values on the left
BBecause right child nodes always have smaller values than their parents
CBecause in BST, left child nodes always have smaller values than their parents
DBecause the minimum value is always at the root
Attempts:
2 left
💡 Hint
Recall the BST property about left and right children values.
❓ Predict Output
expert
2:00remaining
Output of findMin after inserting nodes in BST
What is the output of the following Java code after inserting nodes in this order: 20, 10, 30, 5, 15?
DSA Java
class Node {
    int data;
    Node left, right;
    Node(int data) {
        this.data = data;
        left = right = null;
    }
}

public class BST {
    Node root;

    void insert(int data) {
        root = insertRec(root, data);
    }

    Node insertRec(Node root, int data) {
        if (root == null) {
            root = new Node(data);
            return root;
        }
        if (data < root.data) {
            root.left = insertRec(root.left, data);
        } else if (data > root.data) {
            root.right = insertRec(root.right, data);
        }
        return root;
    }

    int findMin(Node node) {
        if (node == null) return -1;
        while (node.left != null) {
            node = node.left;
        }
        return node.data;
    }

    public static void main(String[] args) {
        BST tree = new BST();
        tree.insert(20);
        tree.insert(10);
        tree.insert(30);
        tree.insert(5);
        tree.insert(15);
        System.out.println(tree.findMin(tree.root));
    }
}
A20
B10
C15
D5
Attempts:
2 left
💡 Hint
The smallest inserted value is the minimum in the BST.

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 parents.
  2. Step 2: Locate minimum element

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

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

    Minimum = leftmost node [OK]
Hint: Minimum is always the leftmost node in BST [OK]
Common Mistakes:
  • Confusing minimum with root node
  • Thinking minimum is rightmost node
  • Assuming minimum can be any leaf
2. Which of the following Java code snippets correctly finds the minimum element in a BST node named root?
easy
A. while(root.right != null) { root = root.right; } return root.data;
B. while(root.left != null) { root = root.left; } return root.data;
C. if(root == null) return Integer.MAX_VALUE; return root.data;
D. 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 left child is null, then returns data.
  3. Final Answer:

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

    Loop left until null, then return [OK]
Hint: Traverse left children until none left [OK]
Common Mistakes:
  • Traversing right child instead of left
  • Returning root data without traversal
  • Ignoring null root check
3. What will be the output of the following Java code snippet?
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);
medium
A. 2
B. 7
C. 5
D. 10

Solution

  1. Step 1: Trace the tree structure

    Root=10, left=5, right=15, left.left=2, left.right=7.
  2. Step 2: Follow while loop to find minimum

    Start at 10, move left to 5, then left to 2, which has no left child.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Leftmost node data = 2 [OK]
Hint: Output is leftmost node's data after loop [OK]
Common Mistakes:
  • Printing root data instead of leftmost
  • Confusing left and right traversal
  • Stopping loop too early
4. Identify the error in the following Java method to find the minimum element in a BST:
public int findMin(Node root) {
  if(root == null) return -1;
  while(root.right != null) {
    root = root.right;
  }
  return root.data;
}
medium
A. Method should be void, not int
B. Should return -1 only if root is leaf
C. Should check for root.data == null
D. Should traverse left child, not right child

Solution

  1. Step 1: Analyze traversal direction

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

    Loop should check root.left != null and move root = root.left.
  3. Final Answer:

    Should traverse left child, not right child -> Option D
  4. Quick Check:

    Minimum = leftmost node, so traverse left [OK]
Hint: Minimum is leftmost, so traverse left child [OK]
Common Mistakes:
  • Traversing right child instead of left
  • Returning -1 incorrectly
  • Checking data for null instead of node
5. Given a BST where some nodes have duplicate values, which approach correctly finds the minimum unique element?
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;
}

What is the main issue with this method?
hard
A. It uses a HashSet unnecessarily for BST
B. It does not handle empty tree correctly
C. It only checks left children, missing other nodes
D. It returns max value instead of min

Solution

  1. Step 1: Understand traversal logic

    Method only moves down left children, ignoring right subtree.
  2. Step 2: Identify missed nodes

    Duplicates or unique values in right subtree are missed.
  3. Step 3: Correct approach

    To find minimum unique, full tree traversal (e.g., inorder) is needed.
  4. Final Answer:

    It only checks left children, missing other nodes -> Option C
  5. Quick Check:

    Partial traversal misses right subtree [OK]
Hint: Traverse entire tree to find minimum unique value [OK]
Common Mistakes:
  • Assuming left path covers all nodes
  • Ignoring duplicates in right subtree
  • Using HashSet without full traversal
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