0
0
DSA C++programming~10 mins

Mirror a Binary Tree in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to swap the left and right child nodes of the root.

DSA C++
void mirror(Node* root) {
    if (root == nullptr) return;
    Node* temp = root->left;
    root->left = root->[1];
    root->right = temp;
}
Drag options to blanks, or click blank then click option'
Aparent
Bleft
Cright
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning root->left to root->left again instead of root->right.
Swapping with a wrong pointer like parent or child.
2fill in blank
medium

Complete the code to recursively mirror the left subtree.

DSA C++
void mirror(Node* root) {
    if (root == nullptr) return;
    mirror(root->[1]);
    mirror(root->right);
    Node* temp = root->left;
    root->left = root->right;
    root->right = temp;
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on root->right twice.
Calling mirror on an invalid pointer like parent.
3fill in blank
hard

Fix the error in the base case check to avoid null pointer dereference.

DSA C++
void mirror(Node* root) {
    if (root == [1]) return;
    mirror(root->left);
    mirror(root->right);
    Node* temp = root->left;
    root->left = root->right;
    root->right = temp;
}
Drag options to blanks, or click blank then click option'
ANULL
Bnullptr
C0
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL which is legacy and less type-safe.
Using 0 or false which are not pointers.
4fill in blank
hard

Fill both blanks to complete the recursive mirror function correctly.

DSA C++
void mirror(Node* root) {
    if (root == [1]) return;
    mirror(root->[2]);
    mirror(root->right);
    Node* temp = root->left;
    root->left = root->right;
    root->right = temp;
}
Drag options to blanks, or click blank then click option'
Anullptr
Bleft
Cright
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong null check values.
Recursing on the wrong child pointer.
5fill in blank
hard

Fill all three blanks to implement the mirror function with both recursive calls and swapping.

DSA C++
void mirror(Node* root) {
    if (root == [1]) return;
    mirror(root->[2]);
    mirror(root->[3]);
    Node* temp = root->left;
    root->left = root->right;
    root->right = temp;
}
Drag options to blanks, or click blank then click option'
Anullptr
Bleft
Cright
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Missing one of the recursive calls.
Incorrect null check.