Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap the left and right children of the root node.
DSA Javascript
function mirrorTree(root) {
if (!root) return null;
const temp = root.left;
root.left = root.[1];
root.right = temp;
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with root.left again instead of root.right.
Using incorrect property names like 'parent' or 'child'.
✗ Incorrect
Swapping root.left with root.right mirrors the root's immediate children.
2fill in blank
mediumComplete the code to recursively mirror the left subtree.
DSA Javascript
function mirrorTree(root) {
if (!root) return null;
root.left = mirrorTree(root.[1]);
root.right = mirrorTree(root.right);
const temp = root.left;
root.left = root.right;
root.right = temp;
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Recursively calling mirrorTree on root.right instead of root.left.
Using incorrect property names.
✗ Incorrect
We recursively call mirrorTree on root.left to mirror the left subtree before swapping.
3fill in blank
hardFix the error in the base case to correctly handle an empty tree.
DSA Javascript
function mirrorTree(root) {
if (root === [1]) return null;
root.left = mirrorTree(root.left);
root.right = mirrorTree(root.right);
const temp = root.left;
root.left = root.right;
root.right = temp;
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for undefined instead of null.
Using 0 or false which are not valid tree node checks.
✗ Incorrect
The base case should check if root is null to stop recursion.
4fill in blank
hardFill both blanks to complete the recursive mirror function correctly.
DSA Javascript
function mirrorTree(root) {
if (root === [1]) return null;
root.left = mirrorTree(root.[2]);
root.right = mirrorTree(root.right);
const temp = root.left;
root.left = root.right;
root.right = temp;
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined instead of null for base case.
Recursively calling mirrorTree on root.right instead of root.left.
✗ Incorrect
Base case checks for null; recursively mirror left subtree using root.left.
5fill in blank
hardFill all three blanks to implement a complete mirror function with recursion and swapping.
DSA Javascript
function mirrorTree(root) {
if (root === [1]) return null;
root.left = mirrorTree(root.[2]);
root.right = mirrorTree(root.[3]);
const temp = root.left;
root.left = root.right;
root.right = temp;
return root;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Missing base case or using undefined.
Not recursively calling mirrorTree on both children.
Not swapping children after recursion.
✗ Incorrect
Check for null base case; recursively mirror left and right subtrees; then swap children.