0
0
DSA Typescriptprogramming~20 mins

Mirror a Binary Tree in DSA Typescript - 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 TypeScript code that mirrors a binary tree and prints its inorder traversal?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function mirrorTree(root: TreeNode | null): TreeNode | null {
  if (!root) return null;
  const left = mirrorTree(root.left);
  const right = mirrorTree(root.right);
  root.left = right;
  root.right = left;
  return root;
}

function inorder(root: TreeNode | null, result: number[] = []): number[] {
  if (!root) return result;
  inorder(root.left, result);
  result.push(root.val);
  inorder(root.right, result);
  return result;
}

const root = new TreeNode(1, new TreeNode(2), new TreeNode(3));
mirrorTree(root);
console.log(inorder(root));
A[3, 1, 2]
B[2, 1, 3]
C[1, 2, 3]
D[3, 2, 1]
Attempts:
2 left
💡 Hint
Think about how inorder traversal visits nodes after mirroring the left and right children.
🧠 Conceptual
intermediate
1:30remaining
Understanding Mirror Tree Recursive Calls
In the mirrorTree function, what is the main purpose of the recursive calls before swapping the children?
ATo swap the left and right children of the current node only
BTo print the values of the nodes in preorder
CTo count the number of nodes in the tree
DTo ensure the entire left and right subtrees are mirrored before swapping
Attempts:
2 left
💡 Hint
Think about what happens if you swap children before mirroring subtrees.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Mirror Tree Implementation
What error will this code produce when trying to mirror a binary tree?
DSA Typescript
function mirrorTree(root: TreeNode | null): TreeNode | null {
  if (!root) return null;
  mirrorTree(root.left);
  mirrorTree(root.right);
  const temp = root.left;
  root.left = root.right;
  root.right = temp;
  return root;
}

const root = new TreeNode(1, new TreeNode(2), new TreeNode(3));
mirrorTree(root);
console.log(root.left?.val);
ANo error; output is 3
BTypeError because root.left or root.right is undefined
CStack overflow due to infinite recursion
DOutput is 2 instead of 3
Attempts:
2 left
💡 Hint
Check if the recursive calls are used correctly and if swapping is done properly.
Predict Output
advanced
2:30remaining
Output of Mirroring a Larger Binary Tree
What is the output of the inorder traversal after mirroring this binary tree?
DSA Typescript
const root = new TreeNode(4,
  new TreeNode(2, new TreeNode(1), new TreeNode(3)),
  new TreeNode(7, new TreeNode(6), new TreeNode(9))
);
mirrorTree(root);
console.log(inorder(root));
A[1, 2, 3, 4, 6, 7, 9]
B[9, 7, 6, 4, 3, 2, 1]
C[6, 7, 9, 4, 1, 2, 3]
D[3, 2, 1, 4, 9, 7, 6]
Attempts:
2 left
💡 Hint
Remember mirroring swaps left and right subtrees at every node.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Mirroring a Binary Tree
What is the time complexity of the mirrorTree function for a binary tree with n nodes?
AO(log n), because the tree is divided in half each recursion
BO(n^2), because each node swaps children multiple times
CO(n), because each node is visited once
DO(1), because swapping is done in constant time
Attempts:
2 left
💡 Hint
Consider how many times the function visits each node.