Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a pointer to the root node of a BST.
DSA C
struct Node* [1] = NULL;Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not represent the root node.
✗ Incorrect
The root pointer is commonly named 'root' to represent the root of the BST.
2fill in blank
mediumComplete the code to move to the left child of a node in the BST.
DSA C
current = current->[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' to move left.
✗ Incorrect
To move to the left child in a BST, use the 'left' pointer.
3fill in blank
hardFix the error in the code to find the inorder predecessor when the left subtree exists.
DSA C
while (temp->[1] != NULL) { temp = temp->right; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' in the while loop condition.
✗ Incorrect
To find the rightmost node in the left subtree, move right until NULL.
4fill in blank
hardFill both blanks to correctly find the inorder predecessor when no left subtree exists.
DSA C
while (current->[1] != NULL && current == current->[2]->left) { current = current->parent; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' to check child relation.
✗ Incorrect
We move up using 'parent' pointer while current is the left child of its parent.
5fill in blank
hardFill all three blanks to complete the function that returns the inorder predecessor of a given node.
DSA C
struct Node* inorderPredecessor(struct Node* root, struct Node* current) {
if (current->[1] != NULL) {
struct Node* temp = current->left;
while (temp->[2] != NULL) {
temp = temp->right;
}
return temp;
}
struct Node* predecessor = NULL;
while (root != NULL) {
if (current->data > root->data) {
predecessor = root;
root = root->[3];
} else {
root = root->left;
}
}
return predecessor;
}Attempts:
3 left
💡 Hint
Common Mistakes
Confusing left and right pointers in traversal.
✗ Incorrect
1) Check if left child exists (current->left).
2) Find rightmost node in left subtree (temp->right).
3) Move right in BST while searching for predecessor (root->right).
