0
0
DSA Javascriptprogramming~10 mins

Mirror a Binary Tree in DSA Javascript - 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 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'
Aright
Bleft
Cchild
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with root.left again instead of root.right.
Using incorrect property names like 'parent' or 'child'.
2fill in blank
medium

Complete 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'
Aright
Bchild
Cparent
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Recursively calling mirrorTree on root.right instead of root.left.
Using incorrect property names.
3fill in blank
hard

Fix 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'
Anull
Bfalse
Cundefined
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for undefined instead of null.
Using 0 or false which are not valid tree node checks.
4fill in blank
hard

Fill 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'
Anull
Bleft
Cright
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined instead of null for base case.
Recursively calling mirrorTree on root.right instead of root.left.
5fill in blank
hard

Fill 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'
Anull
Bleft
Cright
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Missing base case or using undefined.
Not recursively calling mirrorTree on both children.
Not swapping children after recursion.