A developer writes a package manager that ends up installed on most of the Mac laptops in this industry. He interviews at Google. Somebody asks him to invert a binary tree on a whiteboard. He can't do it on the spot. He doesn't get the offer. He posts about it in 2015, and the internet has been arguing about it ever since.
The invert a binary tree interview question is five lines of code. That is exactly why people rage about it, and exactly why it refuses to die.
I've asked tree questions on the other side of that whiteboard for years. The candidates who bombed it almost never bombed on the code. They bombed in the ninety seconds before they wrote any.
TL;DR
- Inverting a binary tree means mirroring it: swap every node's left and right child, recursively. It's ~5 lines.
- The code is not the test. Interviewers score how you handle an underspecified word ("invert"), whether you name the recursion's base case out loud, and whether you can convert it to iteration when pushed.
- Most failures are ambiguity failures. Candidates guess what "invert" means instead of asking, then defend the wrong guess.
- The real separator is the follow-up: do it iteratively, then explain the stack depth on a 1M-node degenerate tree, then explain what mirroring does to a BST's search invariant.
- In 2026, everyone can produce the five lines instantly. The written solution is worth close to zero points. The commentary around it is the entire score.
What does "invert a binary tree" actually mean?
It means mirror it. Every node keeps its value; its left and right subtrees trade places. A tree that read 1 2 3 left to right now reads 3 2 1.
def invert(node):
if node is None:
return None
node.left, node.right = invert(node.right), invert(node.left)
return node
That's it. Python's simultaneous assignment even saves you the temp variable. If you write this in ten seconds and say nothing else, you have scored almost nothing, which is the part nobody tells you.
Why do strong engineers fail the invert a binary tree interview question?
Not because they can't swap two pointers. They fail because "invert" is an ambiguous word and they never say so.
I've watched candidates take "invert" to mean at least four different things:
- Mirror it. The intended answer.
- Flip the parent/child direction. Turn it into a tree where children point at parents. Reasonable reading. Wrong here.
- Invert the ordering. Turn a min-heap-ish structure into a max-heap-ish one by comparing values.
- Invert the values. Negate everything, which is a real thing people have asked and a nonsense thing to guess at.
Reading two is defensible. Guessing between them silently is not. The candidate who says "invert is doing a lot of work in that sentence, do you mean the mirror image, or reversing the edge direction?" has already banked a point before writing anything. The candidate who picks one, builds for six minutes, and then gets told it's the wrong interpretation has now burned half the slot and looks like someone who ships the wrong feature for a sprint.
That's not a trick. That is literally the job. Requirements arrive as one ambiguous verb in a Slack message all the time.
What does the interview question actually test?
Four things, roughly in order of how much they move the scorecard:
Do you probe the spec? Covered above. Highest signal per second in the entire question.
Can you state the base case out loud? "Empty node returns null" is trivial. But a shocking number of people write recursion by muscle memory and cannot articulate why it terminates. Saying it costs three seconds and separates "has internalized recursion" from "has memorized a shape."
Do you notice the evaluation order trap? This version is subtly wrong in a language without simultaneous assignment:
node.left = invert(node.right);
node.right = invert(node.left); // node.left was already overwritten
You just recursed into the already-inverted left subtree and inverted it back. The tree comes out mangled below depth two. Candidates who translate the Python one-liner into Java or C++ line by line walk straight into this. Catching it yourself, before the interviewer says anything, reads as genuinely careful.
Do you know what your recursion costs? Depth is O(h). On a balanced tree that's ~20 frames for a million nodes. On a degenerate, linked-list-shaped tree it's a million frames, and CPython gives up around a thousand by default. That one sentence is worth more than the whole solution.
What's the follow-up that actually separates candidates?
"Now do it without recursion." This is where the question stops being trivia.
from collections import deque
def invert_iterative(root):
if root is None:
return None
q = deque([root])
while q:
node = q.popleft()
node.left, node.right = node.right, node.left
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
return root
Two details a good candidate volunteers here. First: the traversal order doesn't matter at all. BFS, DFS, preorder, postorder, they all produce the same mirrored tree, because the swap at each node is independent of the swaps everywhere else. Say that and you've shown you understand the operation instead of the snippet. Second: swapping the queue for a stack trades peak memory from "widest level" to "deepest path," and which one is cheaper depends entirely on the shape of your tree.
Then the question I actually like, and the one that catches memorizers cold:
"This is a binary search tree. What did you just break?"
You broke the invariant. In a BST, everything left of a node is smaller. Mirror it and everything left is now larger. An in-order traversal that used to emit sorted-ascending now emits sorted-descending, which is sometimes exactly what you wanted. And search still works in O(h), as long as you flip every comparison. That's a real, useful data structure, not a broken one, and watching a candidate reason their way to that in real time is worth more than any amount of clean syntax.
Does this question still work now that AI writes it instantly?
Barely, and only if the interviewer changed how they score it. Any model produces the mirror function perfectly, and every candidate has seen it. The artifact is free now. The reasoning is not.
So the good interviewers I know moved the weight entirely onto the parts a model can't hand you in the room: which interpretation you picked and why, what you noticed about your own code before you were told, and how you answered a follow-up you'd never seen. Some deliberately hand you a working implementation with the Java evaluation-order bug in it and just ask what happens. Interviewers who didn't adapt are still grading the five lines, and they're getting pure noise for it.
If you're on the asking side and your question is still gradeable in 2026, it's because the follow-ups are where the points live.
How should you handle a "trivia" question when you get one?
Slow down for one sentence. That's the whole technique.
- Restate the ask in your own words and name the ambiguity: "Mirror image, right? Not reversing edge direction?"
- Say the base case and the complexity out loud before you write the body.
- After you finish, audit your own code unprompted. One pass. "Evaluation order is fine in Python here, would be a bug in Java."
- Volunteer the constraint that breaks it: "This dies on a deep degenerate tree, want the iterative version?"
Four small habits. They read as seniority, and they cost you maybe thirty seconds.
And if you hit an interviewer grading purely on whether the syntax appeared fast enough, you found out something useful for free. That's data about them, not a verdict on you. The Homebrew story is famous because it's the clearest case ever posted of a company optimizing for the wrong signal in public.
So what does the invert a binary tree interview question really test? Not whether you can swap two pointers. It tests whether you resolve an ambiguous requirement before you build on it, whether you can talk about your own code's failure modes without being prompted, and whether you understand the operation deeply enough to reason about what it does to a data structure's invariants. The five lines are the ticket in. Everything that gets written on the scorecard happens in the conversation around them.
Top comments (0)