Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the minimum value in a BST.
DSA Java
public int findMin(Node root) {
if (root == null) {
return -1; // empty tree
}
Node current = root;
while (current.[1] != null) {
current = current.left;
}
return current.data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking current.right instead of current.left
Returning root.data without traversal
Using recursion without base case
✗ Incorrect
In a BST, the minimum element is found by going as far left as possible. So, we check if current.left is not null.
2fill in blank
mediumComplete the code to recursively find the minimum value in a BST.
DSA Java
public int findMinRecursive(Node root) {
if (root == null) {
return -1; // empty tree
}
if (root.[1] == null) {
return root.data;
}
return findMinRecursive(root.left);
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking right child instead of left
Missing base case for null root
Returning root.data too early
✗ Incorrect
If the left child is null, the current node is the minimum. Otherwise, recurse left.
3fill in blank
hardFix the error in the code to correctly find the minimum element iteratively.
DSA Java
public int findMin(Node root) {
if (root == null) {
return -1;
}
Node current = root;
while (current.[1] != null) {
current = current.right;
}
return current.data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Moving right instead of left
Returning root.data without traversal
Not checking for null root
✗ Incorrect
To find the minimum, we must move left, not right. The code incorrectly moves right.
4fill in blank
hardFill both blanks to create a method that returns the minimum node in a BST.
DSA Java
public Node findMinNode(Node root) {
if (root == null) {
return null;
}
Node current = root;
while (current.[1] != null) {
current = current.[2];
}
return current;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different directions for condition and assignment
Using right instead of left
Returning data instead of node
✗ Incorrect
Both blanks must be 'left' to traverse left children until the minimum node is found.
5fill in blank
hardFill all three blanks to create a recursive method that returns the minimum node in a BST.
DSA Java
public Node findMinNodeRecursive(Node root) {
if (root == null) {
return null;
}
if (root.[1] == null) {
return root;
}
return findMinNodeRecursive(root.[2]);
// The minimum node is in the [3] subtree
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking or recursing right child
Returning root too early
Incorrect comment about subtree
✗ Incorrect
Check if left child is null, recurse left, and minimum is in left subtree.
