Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the minimum value in the BST.
DSA C
int findMin(struct Node* root) {
struct 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 traversing
Using recursion unnecessarily
✗ Incorrect
In a BST, the minimum element is found by going as far left as possible. So, we check current->left until it is NULL.
2fill in blank
mediumComplete the code to handle the case when the BST is empty.
DSA C
int findMin(struct Node* root) {
if (root == [1]) {
return -1; // or some error code
}
struct Node* current = root;
while (current->left != NULL) {
current = current->left;
}
return current->data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking if root == 0 instead of root == NULL
Not handling empty tree case
Returning 0 as minimum which may be incorrect
✗ Incorrect
If the root is NULL, it means the tree is empty, so we return an error code like -1.
3fill in blank
hardFix the error in the code to correctly find the minimum element.
DSA C
int findMin(struct Node* root) {
struct Node* current = root;
while (current->left != NULL) {
current = current->[1];
}
return current->data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Moving to current->right instead of current->left
Using current->parent which is not always available
Returning root->data without traversal
✗ Incorrect
To find the minimum, we must move left, not right. So current = current->left is correct.
4fill in blank
hardFill both blanks to complete the function that finds the minimum node pointer in the BST.
DSA C
struct Node* findMinNode(struct Node* root) {
struct Node* current = root;
while (current->[1] != [2]) {
current = current->left;
}
return current;
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking current->right instead of current->left
Comparing to root instead of NULL
Returning data instead of node pointer
✗ Incorrect
We check current->left != NULL and move left until no left child remains, then return current node pointer.
5fill in blank
hardFill all three blanks to complete the recursive function that finds the minimum value in the BST.
DSA C
int findMinRecursive(struct Node* root) {
if (root == [1]) {
return -1; // empty tree
}
if (root->[2] == [3]) {
return root->data;
}
return findMinRecursive(root->left);
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking root->right instead of root->left
Returning root->data without checking left child
Not handling empty tree case
✗ Incorrect
If root is NULL, tree is empty. If root->left is NULL, root is minimum. Otherwise, recurse left.
