Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Mirror a Binary Tree in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Mirror Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Mirroring a Simple Binary Tree
What is the output of the following code after mirroring the binary tree?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function mirror(root) {
  if (!root) return null;
  const left = mirror(root.left);
  const right = mirror(root.right);
  root.left = right;
  root.right = left;
  return root;
}

// Tree:
//     1
//    / \
//   2   3
//  /     \
// 4       5

const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.right = new Node(5);

mirror(root);

function printTree(node) {
  if (!node) return "null";
  return `${node.value} -> (${printTree(node.left)}, ${printTree(node.right)})`;
}

console.log(printTree(root));
A1 -> (3 -> (5 -> (null, null), null), 2 -> (null, 4 -> (null, null)))
B1 -> (2 -> (4 -> (null, null), null), 3 -> (null, 5 -> (null, null)))
C1 -> (3 -> (null, 5 -> (null, null)), 2 -> (4 -> (null, null), null))
D1 -> (2 -> (null, 4 -> (null, null)), 3 -> (5 -> (null, null), null))
Attempts:
2 left
💡 Hint
Think about swapping the left and right children at every node.
🧠 Conceptual
intermediate
1:30remaining
Understanding Mirror Operation on Binary Trees
Which statement best describes what happens when you mirror a binary tree?
AOnly the leaf nodes are swapped with their siblings.
BEach node's left and right children are swapped recursively throughout the tree.
CThe tree is converted into a linked list by flattening all nodes to the right.
DThe values of nodes are reversed in level order without changing the structure.
Attempts:
2 left
💡 Hint
Focus on what happens to the children of each node.
🔧 Debug
advanced
2:00remaining
Identify the Error in Mirror Function
What error will the following mirror function cause when run on a binary tree?
DSA Javascript
function mirror(root) {
  if (!root) return;
  mirror(root.left);
  mirror(root.right);
  let temp = root.left;
  root.left = root.right;
  root.right = temp;
}

// Tree setup omitted for brevity
AThe function returns undefined and does not update the tree.
BTypeError because root.left or root.right might be undefined.
CNo error; function works correctly and mirrors the tree.
DStack overflow due to infinite recursion.
Attempts:
2 left
💡 Hint
Check if the function swaps children after recursive calls.
❓ Predict Output
advanced
2:30remaining
Output After Mirroring a Larger Tree
What is the output of the following code after mirroring the binary tree?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function mirror(root) {
  if (!root) return null;
  const left = mirror(root.left);
  const right = mirror(root.right);
  root.left = right;
  root.right = left;
  return root;
}

// Tree:
//       10
//      /  \
//     5    15
//    / \   / \
//   3   7 12  20

const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.right = new Node(7);
root.right.left = new Node(12);
root.right.right = new Node(20);

mirror(root);

function printTree(node) {
  if (!node) return "null";
  return `${node.value} -> (${printTree(node.left)}, ${printTree(node.right)})`;
}

console.log(printTree(root));
A10 -> (15 -> (20 -> (null, null), 12 -> (null, null)), 5 -> (7 -> (null, null), 3 -> (null, null)))
B10 -> (5 -> (3 -> (null, null), 7 -> (null, null)), 15 -> (12 -> (null, null), 20 -> (null, null)))
C10 -> (15 -> (12 -> (null, null), 20 -> (null, null)), 5 -> (3 -> (null, null), 7 -> (null, null)))
D10 -> (5 -> (7 -> (null, null), 3 -> (null, null)), 15 -> (20 -> (null, null), 12 -> (null, null)))
Attempts:
2 left
💡 Hint
Remember to swap left and right children at every node.
🧠 Conceptual
expert
1:30remaining
Effect of Mirroring Twice on a Binary Tree
What is the result of mirroring a binary tree twice in a row?
AThe tree's node values are reversed in in-order traversal.
BThe tree becomes a linked list skewed to the right.
CThe tree becomes completely empty (null).
DThe tree returns to its original structure.
Attempts:
2 left
💡 Hint
Think about what happens when you swap left and right children twice.

Practice

(1/5)
1. What does it mean to mirror a binary tree?
easy
A. Sort the nodes in ascending order
B. Swap the left and right children of every node in the tree
C. Convert the tree into a linked list
D. Remove all leaf nodes from the tree

Solution

  1. Step 1: Understand the mirror operation

    Mirroring a binary tree means swapping the left and right children of every node.
  2. Step 2: Apply the definition to the tree

    By swapping children at every node, the tree becomes a mirror image of the original.
  3. Final Answer:

    Swap the left and right children of every node in the tree -> Option B
  4. Quick Check:

    Mirror = Swap children [OK]
Hint: Mirror means swapping left and right children at all nodes [OK]
Common Mistakes:
  • Thinking mirror means deleting nodes
  • Confusing mirror with sorting
  • Assuming mirror flattens the tree
2. Which of the following is the correct JavaScript syntax to swap the left and right children of a node named node?
easy
A. let temp = node.left; node.left = node.right; node.right = temp;
B. node.left = node.right; node.right = node.left;
C. node.left, node.right = node.right, node.left;
D. swap(node.left, node.right);

Solution

  1. Step 1: Understand swapping variables in JavaScript

    To swap two variables, use a temporary variable to hold one value during the swap.
  2. Step 2: Check each option

    let temp = node.left; node.left = node.right; node.right = temp; correctly uses a temporary variable to swap node.left and node.right. node.left = node.right; node.right = node.left; overwrites node.left before assigning node.right. node.left, node.right = node.right, node.left; uses invalid syntax in JavaScript. swap(node.left, node.right); calls an undefined function.
  3. Final Answer:

    let temp = node.left; node.left = node.right; node.right = temp; -> Option A
  4. Quick Check:

    Use temp variable to swap [OK]
Hint: Use a temp variable to swap two values in JavaScript [OK]
Common Mistakes:
  • Overwriting one child before saving it
  • Using Python-style tuple swap syntax
  • Calling undefined swap functions
3. What is the output of the following code that mirrors a binary tree?
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function mirror(node) {
  if (!node) return null;
  let left = mirror(node.left);
  let right = mirror(node.right);
  node.left = right;
  node.right = left;
  return node;
}

// Tree before mirroring:
//     1
//    / \
//   2   3
//  /     \
// 4       5

let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.right = new Node(5);

mirror(root);

function printTree(node) {
  if (!node) return 'null';
  return node.val + ' -> ' + printTree(node.left) + ', ' + printTree(node.right);
}

console.log(printTree(root));
medium
A. 1 -> 2 -> 4 -> null, 3 -> 5 -> null, null
B. 1 -> 3 -> 4 -> null, 2 -> 5 -> null, null
C. 1 -> 3 -> null, 2 -> 5 -> null, 4
D. 1 -> 3 -> 5 -> null, 2 -> 4 -> null, null

Solution

  1. Step 1: Understand the mirror function

    The mirror function swaps left and right children recursively for every node.
  2. Step 2: Trace the tree after mirroring

    Original tree: 1 with left=2 and right=3; 2 has left=4; 3 has right=5.
    After mirroring:
    1's left becomes 3, right becomes 2.
    3's left becomes 5, right becomes null.
    2's left becomes null, right becomes 4.
    Printing in preorder with left then right shows: 1 -> 3 -> 5 -> null, 2 -> 4 -> null, null.
  3. Final Answer:

    1 -> 3 -> 5 -> null, 2 -> 4 -> null, null -> Option D
  4. Quick Check:

    Mirror swaps children recursively [OK]
Hint: Trace swaps at each node recursively [OK]
Common Mistakes:
  • Not swapping children at all nodes
  • Mixing left and right children incorrectly
  • Misreading the print output format
4. Find the error in the following mirror function and select the correct fix:
function mirror(node) {
  if (node == null) return;
  mirror(node.left);
  mirror(node.right);
  let temp = node.left;
  node.left = node.right;
  node.right = temp;
}
medium
A. The function should return the node after swapping children
B. The base case should check for undefined instead of null
C. The swap should happen before recursive calls
D. The temp variable should be declared outside the function

Solution

  1. Step 1: Analyze the function behavior

    The function swaps children after recursive calls but does not return the node, so the caller gets undefined.
  2. Step 2: Identify the fix

    Returning the node after swapping ensures the mirrored subtree is returned properly for further use.
  3. Final Answer:

    The function should return the node after swapping children -> Option A
  4. Quick Check:

    Return node after swap [OK]
Hint: Always return the node after modifying it in recursion [OK]
Common Mistakes:
  • Not returning the node causes undefined results
  • Swapping before recursion breaks logic
  • Misunderstanding null vs undefined checks
5. Given a binary tree where some nodes have only one child, how does the mirror function handle these nodes?
Consider the tree:
    10
   /  
  20   
    \
     30

What will be the structure after mirroring?
hard
A. 10 with left child 30 and right child 20; 20 and 30 have no children
B. 10 with left child 20 and right null; 20 with left null and right 30
C. 10 with left child null and right child 20; 20 with left child 30 and right null
D. 10 with left child null and right child 30; 30 with left child 20 and right null

Solution

  1. Step 1: Understand mirroring nodes with single children

    Mirroring swaps left and right children even if one child is null.
  2. Step 2: Apply mirror to given tree

    Original:
    10 has left=20, right=null
    20 has left=null, right=30
    After mirror:
    10's left becomes null, right becomes 20
    20's left becomes 30, right becomes null
  3. Final Answer:

    10 with left child null and right child 20; 20 with left child 30 and right null -> Option C
  4. Quick Check:

    Mirror swaps children even if one is null [OK]
Hint: Swap children even if one child is missing [OK]
Common Mistakes:
  • Ignoring null children during swap
  • Assuming children move to wrong side
  • Confusing node values with positions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard