Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the minimum node in a BST subtree.
DSA C
struct Node* findMin(struct Node* root) {
while (root && root->[1] != NULL) {
root = root->left;
}
return root;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' to find the minimum node.
Checking for NULL incorrectly causing infinite loops.
✗ Incorrect
To find the minimum node in a BST subtree, we keep going to the left child until there is no left child.
2fill in blank
mediumComplete the code to find the inorder successor when the node has a right child.
DSA C
struct Node* inorderSuccessor(struct Node* root, struct Node* n) {
if (n->right != NULL) {
return findMin(n->[1]);
}
// Other cases not shown
return NULL;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' when calling findMin.
Not checking if the right child exists before calling findMin.
✗ Incorrect
If the node has a right child, the inorder successor is the minimum node in the right subtree.
3fill in blank
hardFix the error in the loop that finds the inorder successor when the node has no right child.
DSA C
struct Node* inorderSuccessor(struct Node* root, struct Node* n) {
struct Node* successor = NULL;
while (root != NULL) {
if (n->data < root->data) {
successor = root;
root = root->[1];
} else {
root = root->right;
}
}
return successor;
}Attempts:
3 left
💡 Hint
Common Mistakes
Moving right instead of left in the if condition.
Not updating the successor pointer correctly.
✗ Incorrect
When the node's data is less than root's data, we move to the left child to find a closer successor.
4fill in blank
hardFill both blanks to complete the inorder successor function for all cases.
DSA C
struct Node* inorderSuccessor(struct Node* root, struct Node* n) {
if (n->right != NULL) {
return findMin(n->[1]);
}
struct Node* successor = NULL;
while (root != NULL) {
if (n->data < root->data) {
successor = root;
root = root->[2];
} else {
root = root->right;
}
}
return successor;
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right pointers in the code.
Not handling the case when the node has a right child.
✗ Incorrect
If the node has a right child, find the minimum in the right subtree (right). Otherwise, move left in the tree to find the successor.
5fill in blank
hardFill all three blanks to complete the function that finds the inorder successor using parent pointers.
DSA C
struct Node* inorderSuccessorWithParent(struct Node* n) {
if (n->right != NULL) {
return findMin(n->[1]);
}
struct Node* p = n->[2];
while (p != NULL && n == p->[3]) {
n = p;
p = p->[2];
}
return p;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using left or right instead of parent to move up.
Incorrectly checking child relationship in the while loop.
✗ Incorrect
If the node has a right child, find the minimum in the right subtree (right). Otherwise, move up using parent pointers until the node is a left child, then return that parent.
