Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the maximum value in the BST.
DSA C
int findMax(struct Node* root) {
if (root == NULL) {
return -1; // Tree is empty
}
struct Node* current = root;
while (current->[1] != NULL) {
current = current->right;
}
return current->data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' pointer.
Returning root->data without traversal.
✗ Incorrect
In a BST, the maximum element is the rightmost node. So we keep moving to the right child until it is NULL.
2fill in blank
mediumComplete the recursive function to find the maximum element in the BST.
DSA C
int findMaxRecursive(struct Node* root) {
if (root == NULL) {
return -1;
}
if (root->[1] == NULL) {
return root->data;
}
return findMaxRecursive(root->right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking left child instead of right child.
Not handling NULL root case.
✗ Incorrect
If the right child is NULL, the current node is the maximum. Otherwise, recurse on the right child.
3fill in blank
hardFix the error in the iterative maximum finder function.
DSA C
int findMaxIterative(struct Node* root) {
struct Node* current = root;
while (current->[1] != NULL) {
current = current->right;
}
return current->data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using left pointer to find maximum.
Not checking for NULL root.
✗ Incorrect
To find the maximum, we must move to the right child, not left.
4fill in blank
hardFill both blanks to complete the recursive maximum finder with base case and recursive call.
DSA C
int findMax(struct Node* root) {
if (root == NULL) {
return [1];
}
if (root->right == NULL) {
return root->[2];
}
return findMax(root->right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of -1 for empty tree.
Returning left child data instead of node data.
✗ Incorrect
Return -1 for empty tree. Return root->data if no right child.
5fill in blank
hardFill all three blanks to create a function that finds the maximum element iteratively and handles empty tree.
DSA C
int findMaxElement(struct Node* root) {
if (root == NULL) {
return [1];
}
struct Node* temp = root;
while (temp->[2] != NULL) {
temp = temp->[3];
}
return temp->data;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using left pointer instead of right.
Not handling empty tree case.
✗ Incorrect
Return -1 if tree is empty. Traverse right child until NULL to find max.
