0
0
DSA Javascriptprogramming~20 mins

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

Choose your learning style9 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.