0
0
DSA Typescriptprogramming~10 mins

Mirror a Binary Tree in DSA Typescript - 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 Typescript
function mirrorTree(root: TreeNode | null): TreeNode | null {
  if (root === null) return null;
  const temp = root.left;
  root.left = [1];
  root.right = temp;
  return root;
}
Drag options to blanks, or click blank then click option'
Aroot.left
Broot.right
Cnull
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning root.left to itself instead of root.right.
Assigning null instead of the right child.
2fill in blank
medium

Complete the code to recursively mirror the left subtree.

DSA Typescript
function mirrorTree(root: TreeNode | null): TreeNode | null {
  if (root === null) return null;
  const temp = root.left;
  root.left = [1];
  root.right = mirrorTree(temp);
  return root;
}
Drag options to blanks, or click blank then click option'
AmirrorTree(root.right)
Broot.right
Croot.left
DmirrorTree(root.left)
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirrorTree on root.left instead of root.right.
Assigning root.left directly without recursion.
3fill in blank
hard

Fix the error in the recursive calls to mirror both subtrees correctly.

DSA Typescript
function mirrorTree(root: TreeNode | null): TreeNode | null {
  if (root === null) return null;
  const leftMirrored = mirrorTree(root.left);
  const rightMirrored = [1];
  root.left = rightMirrored;
  root.right = leftMirrored;
  return root;
}
Drag options to blanks, or click blank then click option'
AmirrorTree(root.right)
BmirrorTree(root.left)
Croot.left
Droot.right
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirrorTree on root.left twice.
Assigning root.left or root.right directly without recursion.
4fill in blank
hard

Fill both blanks to complete the recursive mirror function that swaps children after mirroring.

DSA Typescript
function mirrorTree(root: TreeNode | null): TreeNode | null {
  if (root === null) return null;
  const leftMirrored = [1];
  const rightMirrored = [2];
  root.left = rightMirrored;
  root.right = leftMirrored;
  return root;
}
Drag options to blanks, or click blank then click option'
AmirrorTree(root.left)
BmirrorTree(root.right)
Croot.left
Droot.right
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping without recursion.
Calling mirrorTree on the same child twice.
5fill in blank
hard

Fill both blanks to create a one-liner recursive mirror function using destructuring assignment.

DSA Typescript
function mirrorTree(root: TreeNode | null): TreeNode | null {
  if (root === null) return null;
  [root.left, root.right] = [[1], [2]];
  return root;
}
Drag options to blanks, or click blank then click option'
AmirrorTree(root.right)
BmirrorTree(root.left)
Croot.left
Droot.right
Attempts:
3 left
💡 Hint
Common Mistakes
Not swapping the children correctly.
Using non-recursive values in the assignment.