30. Invert Binary Tree
easyAsked at PlaidInvert (mirror) a binary tree. Plaid asks this as a recursion warm-up before harder category-tree transformations.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Plaid loops.
- Glassdoor (2025)— Plaid intro problem.
- LeetCode Discuss (2026)— Plaid OA warm-up.
Problem
Given the root of a binary tree, invert the tree, and return its root. Inverting means swapping every left subtree with its corresponding right subtree.
Constraints
The number of nodes in the tree is in the range [0, 100].-100 <= Node.val <= 100
Examples
Example 1
root = [4,2,7,1,3,6,9][4,7,2,9,6,3,1]Example 2
root = [][]Approaches
1. Iterative BFS swap
BFS through the tree; at each node, swap children.
- Time
- O(n)
- Space
- O(w)
function invertTree(root) {
if (!root) return null;
const q = [root];
while (q.length) {
const n = q.shift();
[n.left, n.right] = [n.right, n.left];
if (n.left) q.push(n.left);
if (n.right) q.push(n.right);
}
return root;
}Tradeoff: Avoids recursion stack. shift() is O(n) — use an index pointer or a deque in production.
2. Recursive
Swap children, recurse on each.
- Time
- O(n)
- Space
- O(h)
function invertTree(root) {
if (!root) return null;
[root.left, root.right] = [invertTree(root.right), invertTree(root.left)];
return root;
}Tradeoff: One-line core. The destructuring assignment makes the swap explicit.
Plaid-specific tips
Plaid uses this as a recursion sanity check. Bonus signal: ask whether the input may be huge — if yes, default to the iterative version. Mention that the same pattern shows up when mirroring a category-tree for a left-handed display layout.
Common mistakes
- Swapping before recursing instead of using destructuring — works but easier to introduce bugs.
- Returning a new tree — the problem allows in-place mutation, which is cheaper.
- Forgetting the empty-tree base case.
Follow-up questions
An interviewer at Plaid may pivot to one of these next:
- Detect if a tree is symmetric (LC 101) — closely related.
- Invert only one level of the tree.
- Same problem on N-ary trees.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Recursive or iterative — which does Plaid prefer?
Either is fine for a 100-node tree. For 10M-node category trees, iterative avoids stack overflow.
Why does destructuring help?
It does the swap in one statement, eliminating the need for a temporary variable and a wrong-order bug.
Practice these live with InterviewChamp.AI
Drill Invert Binary Tree and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →