Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the minimum value in a BST by going left.
DSA C++
int findMin(Node* root) {
while (root->[1] != nullptr) {
root = root->left;
}
return root->data;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' to find the minimum.
Trying to access a non-existent 'parent' pointer.
✗ Incorrect
In a BST, the minimum element is found by continuously moving to the left child until there is no left child.
2fill in blank
mediumComplete the code to handle the case when the BST is empty.
DSA C++
int findMin(Node* root) {
if (root == [1]) {
return -1; // or some error code
}
while (root->left != nullptr) {
root = root->left;
}
return root->data;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or NULL instead of nullptr in modern C++.
Not checking for empty tree before accessing nodes.
✗ Incorrect
We check if the root pointer is nullptr to know if the tree is empty.
3fill in blank
hardFix the error in the recursive version of findMin function.
DSA C++
int findMin(Node* root) {
if (root == nullptr) {
return -1;
}
if (root->[1] == nullptr) {
return root->data;
}
return findMin(root->left);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking right child instead of left child.
Using an incorrect pointer name.
✗ Incorrect
The base case checks if the left child is nullptr to find the minimum node.
4fill in blank
hardFill both blanks to complete the iterative findMin function with a for loop.
DSA C++
int findMin(Node* root) {
for (; root != [1]; root = root->[2]) {
if (root->left == nullptr) {
break;
}
}
return root->data;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of nullptr.
Moving to the right child instead of left.
✗ Incorrect
The loop continues until root->left is nullptr, moving left each time to find the minimum.
5fill in blank
hardFill all three blanks to create a recursive function that returns the minimum value in a BST.
DSA C++
int findMin(Node* root) {
if (root == [1]) {
return -1;
}
if (root->[2] == [3]) {
return root->data;
}
return findMin(root->left);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking right child instead of left child.
Not checking for empty tree first.
✗ Incorrect
Check if root is nullptr first, then if left child is nullptr to find minimum recursively.