~/boss / magic-mirror

Invert a binary tree (recursion)

The Mirror

Flip the tree into its reflection. One swap per node, in the order the recursion calls them.

Loading the fight…

How to beat it

At each node, swap its left and right child. Then do the same to the left subtree, then the right one. A leaf has nothing to flip: its call just returns. The call stack remembers who is still waiting.

Now the real boss

Interviews ask for the code. Watch it run, then write it yourself, tested in Python, C++ or Java.

On your phone? Writing code is easier on a laptop: open bossfight.dev/problems/flip-tree-left-to-right there. Sign in with your email to keep your progress on every device.

More bosses

esc