Mirror a Binary Tree in DSA Javascript - Time & Space Complexity
We want to understand how the time needed to mirror a binary tree changes as the tree grows.
How does the work increase when the tree has more nodes?
Analyze the time complexity of the following code snippet.
function mirrorTree(node) {
if (node === null) return null;
const leftMirrored = mirrorTree(node.left);
const rightMirrored = mirrorTree(node.right);
node.left = rightMirrored;
node.right = leftMirrored;
return node;
}
This code swaps the left and right children of every node in the tree, creating a mirror image.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call on each node to swap children.
- How many times: Once per node in the tree.
Each node is visited once, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 swaps and recursive calls |
| 100 | About 100 swaps and recursive calls |
| 1000 | About 1000 swaps and recursive calls |
Pattern observation: The operations increase linearly as the tree size grows.
Time Complexity: O(n)
This means the time to mirror the tree grows directly with the number of nodes.
[X] Wrong: "Mirroring only changes the top nodes, so it takes constant time."
[OK] Correct: Every node must be visited and swapped, so the time depends on the total nodes, not just the top.
Understanding this helps you explain how recursive tree algorithms scale, a key skill in many coding challenges.
"What if we changed the tree to be a linked list (all nodes only have one child)? How would the time complexity change?"
Practice
Solution
Step 1: Understand the mirror operation
Mirroring a binary tree means swapping the left and right children of every node.Step 2: Apply the definition to the tree
By swapping children at every node, the tree becomes a mirror image of the original.Final Answer:
Swap the left and right children of every node in the tree -> Option BQuick Check:
Mirror = Swap children [OK]
- Thinking mirror means deleting nodes
- Confusing mirror with sorting
- Assuming mirror flattens the tree
node?Solution
Step 1: Understand swapping variables in JavaScript
To swap two variables, use a temporary variable to hold one value during the swap.Step 2: Check each option
let temp = node.left; node.left = node.right; node.right = temp; correctly uses a temporary variable to swapnode.leftandnode.right. node.left = node.right; node.right = node.left; overwritesnode.leftbefore assigningnode.right. node.left, node.right = node.right, node.left; uses invalid syntax in JavaScript. swap(node.left, node.right); calls an undefined function.Final Answer:
let temp = node.left; node.left = node.right; node.right = temp; -> Option AQuick Check:
Use temp variable to swap [OK]
- Overwriting one child before saving it
- Using Python-style tuple swap syntax
- Calling undefined swap functions
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));Solution
Step 1: Understand the mirror function
The mirror function swaps left and right children recursively for every node.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.Final Answer:
1 -> 3 -> 5 -> null, 2 -> 4 -> null, null -> Option DQuick Check:
Mirror swaps children recursively [OK]
- Not swapping children at all nodes
- Mixing left and right children incorrectly
- Misreading the print output format
function mirror(node) {
if (node == null) return;
mirror(node.left);
mirror(node.right);
let temp = node.left;
node.left = node.right;
node.right = temp;
}Solution
Step 1: Analyze the function behavior
The function swaps children after recursive calls but does not return the node, so the caller gets undefined.Step 2: Identify the fix
Returning the node after swapping ensures the mirrored subtree is returned properly for further use.Final Answer:
The function should return the node after swapping children -> Option AQuick Check:
Return node after swap [OK]
- Not returning the node causes undefined results
- Swapping before recursion breaks logic
- Misunderstanding null vs undefined checks
Consider the tree:
10
/
20
\
30What will be the structure after mirroring?
Solution
Step 1: Understand mirroring nodes with single children
Mirroring swaps left and right children even if one child is null.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 nullFinal Answer:
10 with left child null and right child 20; 20 with left child 30 and right null -> Option CQuick Check:
Mirror swaps children even if one is null [OK]
- Ignoring null children during swap
- Assuming children move to wrong side
- Confusing node values with positions
Ready to prove it?
Pass the test and this pattern moves unknown
