Iron-On Transfer Flip
A print shop is making family-reunion T-shirts. The design is a family tree of boxes, each holding a person's id, with at most two children under each box. Iron-on transfer paper prints mirror-image, so the design must be flipped left to right first. Flipping does more than swap the two halves at the top: every branch, all the way down, ends up the other way round.
Your task
Write mirrorDesign(root). Turn the tree into its mirror image: at every node, the left subtree becomes the right subtree and the right becomes the left. Return the root of the mirrored tree. You may change the tree in place.
An empty tree stays empty. Trees are shown in level order, like [5, 3, 8, 1, 4, null, 9], where null marks a missing child. Your function receives and returns real TreeNode objects with val, left and right.
mirrorDesign(root: TreeNode) → TreeNode
Examples
root = [5, 3, 8, 1, 4, null, 9]
[5, 8, 3, 9, null, 4, 1]
5 stays on top and its children swap: 8 left, 3 right. Below them the same happens: 9 moves to 8's left, and 3's children become 4 then 1. The bottom row 1, 4, 9 now reads 9, 4, 1.
root = [1, 2]
[1, null, 2]
A lone left child becomes a lone right child, so the swap must also work when the other child is missing.
Constraints
- 0 ≤ number of people ≤ 1,000
- -100,000 ≤ id ≤ 100,000
- The tree is at most 500 levels deep.
Start with brute force
You could read the tree into rows, reverse each row, and rebuild. But that means keeping every empty slot, and the slots double at every level, so a lopsided tree with 1,000 people can have rows far bigger than the tree. You never need the rows: swapping the two child pointers at every node, once, flips the whole picture in O(n) with no copies.
Hints
0 of 4 shown- Hint 1
Hold the first example up to a mirror. The top box stays put. Where does box 1, bottom-left, end up?
- Hint 2
Box 1 moves to the bottom-right. That needs 3 to move to the right side and 1 to move right under 3. So the swap happens at every node, not only the top.
- Hint 3
Each subtree is a smaller design that also needs mirroring. If both subtrees were already mirrored, what would be left to do at the root?
- Hint 4
For a null node return null. Otherwise mirror both subtrees, then attach the mirrored right as the new left child and the mirrored left as the new right child. Save one side in a variable before overwriting it.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
A mirrored tree is the root, with the mirrored right subtree on its left and the mirrored left subtree on its right.
Trace [5, 3, 8, 1, 4, null, 9]:
- Mirror the subtree at 3. Leaves 1 and 4 mirror to themselves, then swap: 3 has 4 on the left and 1 on the right.
- Mirror the subtree at 8. Its only child 9 moves from right to left.
- At 5, swap the finished subtrees: 8 goes left, 3 goes right.
The result, level by level, is [5, 8, 3, 9, null, 4, 1]. Writing root.left = mirror(root.right) without saving the original left first would lose that subtree before you mirrored it.
function mirrorDesign(root) {
if (root === null) return null;
const left = mirrorDesign(root.left); // mirror each side first
const right = mirrorDesign(root.right);
root.left = right; // then swap them
root.right = left;
return root;
}def mirror_design(root):
if root is None:
return None
left = mirror_design(root.left) # mirror each side first
right = mirror_design(root.right)
root.left = right # then swap them
root.right = left
return rootRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.