Pattern this teaches:Tree DFS
Invert Binary Tree
An easy Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Binary Trees
- Sheets
- 3
- Core for
- 4 roles
The problem
Mirror a binary tree: at every node, the left child and the right child trade places. Return the root of the mirrored tree.
Example 1
- Input
- [5,3,7,1,4,6,8]
- Output
- [5,7,3,8,6,4,1]
- Why
- Swap left/right at every node: 3<->7, 1<->4, 6<->8.
Example 2
- Input
- [10,4,15]
- Output
- [10,15,4]
- Why
- The two children swap sides.
Example 3
- Input
- []
- Output
- []
- Why
- An empty tree remains empty.
Constraints
- The number of nodes is in the range [0, 104].
- -100 <= Node.val <= 100
How to think about it
Updated 2026-09-29Inversion is purely a local permutation repeated everywhere: every single parent exchanges its left child pointer with its right child pointer. Once the swap is performed at the root and recursively applied across subtrees, the entire global reflection resolves automatically.
Approaches, worst first
Iterative queue or stack walk
time O(n) · space O(w)
Enqueue the root. While the queue is non-empty, dequeue a node, swap its left and right pointers, and push any non-null children. Safe from call stack depth limits at the expense of heap queue allocations.
Depth-first recursive swapWrite this one
time O(n) · space O(h)
Base case returns null. Otherwise, temporarily store left and right subtrees, swap them onto the node, and recurse into both branches. Modifies the pointers in place and returns the root.
Where people lose marks · 3
- Overwriting `node.left` before saving it: writing `node.left = invert(node.right)` destroys the original left reference before the right child can be inverted.
- Failing to handle an empty root gracefully; calling child accessors on a null input throws an unhandled exception.
- Swapping only the values instead of the child pointers breaks when trees are unbalanced or non-isomorphic.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
More Binary Trees problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .