Challenge - 5 Problems
Mirror Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after mirroring a simple binary tree
What is the output of the in-order traversal after mirroring the binary tree?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class MirrorTree { public static void mirror(Node root) { if (root == null) return; Node temp = root.left; root.left = root.right; root.right = temp; mirror(root.left); mirror(root.right); } public static void inorder(Node root) { if (root == null) return; inorder(root.left); System.out.print(root.val + " "); inorder(root.right); } public static void main(String[] args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); mirror(root); inorder(root); } }
Attempts:
2 left
💡 Hint
Remember that mirroring swaps left and right children at every node.
✗ Incorrect
The original tree in-order is 4 2 5 1 3. After mirroring, left and right children swap, so in-order becomes 3 1 5 2 4.
❓ Predict Output
intermediateOutput of pre-order traversal after mirroring
What is the output of the pre-order traversal after mirroring the binary tree below?
Tree:
10
/ \
20 30
/ / \
40 50 60
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class MirrorTree { public static void mirror(Node root) { if (root == null) return; Node temp = root.left; root.left = root.right; root.right = temp; mirror(root.left); mirror(root.right); } public static void preorder(Node root) { if (root == null) return; System.out.print(root.val + " "); preorder(root.left); preorder(root.right); } public static void main(String[] args) { Node root = new Node(10); root.left = new Node(20); root.right = new Node(30); root.left.left = new Node(40); root.right.left = new Node(50); root.right.right = new Node(60); mirror(root); preorder(root); } }
Attempts:
2 left
💡 Hint
Pre-order visits root, then left subtree, then right subtree after mirroring.
✗ Incorrect
Mirroring swaps left and right children. Original pre-order is 10 20 40 30 50 60. After mirroring, left and right children swap, so pre-order becomes 10 30 60 50 20 40.
🔧 Debug
advancedIdentify the bug in the mirror function
The following mirror function is intended to mirror a binary tree. What is the bug that causes incorrect mirroring?
DSA Java
public void mirror(Node root) {
if (root == null) return;
mirror(root.left);
mirror(root.right);
Node temp = root.left;
root.left = root.right;
root.right = temp;
}Attempts:
2 left
💡 Hint
Think about when children are swapped relative to recursion.
✗ Incorrect
Swapping children after recursive calls means recursion works on original children, not swapped ones, causing incorrect mirror.
🧠 Conceptual
advancedTime complexity of mirroring a binary tree
What is the time complexity of the mirror function that recursively swaps left and right children of every node in a binary tree with n nodes?
Attempts:
2 left
💡 Hint
Consider how many nodes the function visits and how much work per node.
✗ Incorrect
The mirror function visits each node once and swaps children in constant time, so total time is O(n).
🚀 Application
expertMirror a binary tree iteratively
Which of the following Java code snippets correctly mirrors a binary tree using an iterative approach with a queue?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class MirrorTreeIterative { public static void mirror(Node root) { // Fill in the correct iterative mirroring code } }
Attempts:
2 left
💡 Hint
Use a queue for level order traversal and swap children immediately after dequeue.
✗ Incorrect
Option A uses a queue for breadth-first traversal and swaps children immediately, correctly mirroring the tree iteratively.
