Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap the left and right child nodes of the root.
DSA C
void mirror(struct Node* root) {
if (root == NULL) return;
struct Node* temp = root->[1];
root->left = root->right;
root->right = temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using root->right instead of root->left for temp.
Trying to swap data instead of child pointers.
✗ Incorrect
We store the left child in temp before swapping. So temp must be root->left.
2fill in blank
mediumComplete the code to recursively call mirror on the left subtree.
DSA C
void mirror(struct Node* root) {
if (root == NULL) return;
// Swap children
struct Node* temp = root->left;
root->left = root->right;
root->right = temp;
mirror(root->[1]);
mirror(root->right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on root->right twice.
Calling mirror on root->data or root->parent.
✗ Incorrect
After swapping, we call mirror on the new left subtree, which was originally the right subtree.
3fill in blank
hardFix the error in the base case check to stop recursion.
DSA C
void mirror(struct Node* root) {
if (root == [1]) return;
struct Node* temp = root->left;
root->left = root->right;
root->right = temp;
mirror(root->left);
mirror(root->right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking if root == 0 instead of NULL.
Checking if root == false or root itself.
✗ Incorrect
The base case checks if root is NULL to stop recursion.
4fill in blank
hardFill both blanks to complete the recursive calls for mirroring both subtrees.
DSA C
void mirror(struct Node* root) {
if (root == NULL) return;
struct Node* temp = root->left;
root->left = root->right;
root->right = temp;
mirror(root->[1]);
mirror(root->[2]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on data or parent instead of child pointers.
Calling mirror on the same child twice.
✗ Incorrect
We recursively call mirror on both left and right children after swapping.
5fill in blank
hardFill all three blanks to complete the mirror function with base case, swap, and recursive calls.
DSA C
void mirror(struct Node* root) {
if (root == [1]) return;
struct Node* temp = root->[2];
root->left = root->[3];
root->right = temp;
mirror(root->left);
mirror(root->right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using data or wrong pointers for swapping.
Missing the base case or swapping incorrectly.
✗ Incorrect
Base case checks for NULL, temp stores left child, then left is assigned right child.
