Challenge - 5 Problems
BST Minimum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind 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)); } }
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
✗ Incorrect
The code traverses the left children starting from the root until it reaches the leftmost node, which holds the minimum value 2.
❓ Predict Output
intermediateFind 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)); } }
Attempts:
2 left
💡 Hint
Check the base case when the node is null.
✗ Incorrect
The code returns -1 explicitly when the input node is null, indicating the tree is empty.
🔧 Debug
advancedIdentify 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)); } }
Attempts:
2 left
💡 Hint
What happens if node is null and you access node.left?
✗ Incorrect
The code does not check if node is null before accessing node.left, so it throws NullPointerException when tree is empty.
🧠 Conceptual
advancedWhy 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?
Attempts:
2 left
💡 Hint
Recall the BST property about left and right children values.
✗ Incorrect
In a BST, left children contain values smaller than their parent node, so the minimum value is found by going left until no more left child exists.
❓ Predict Output
expertOutput 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)); } }
Attempts:
2 left
💡 Hint
The smallest inserted value is the minimum in the BST.
✗ Incorrect
The inserted nodes create a BST where 5 is the leftmost node, so findMin returns 5.
