Binary Trees · 25 of 35
Check if 2 trees are mirror or not
Check if one tree is the reflection of the other, by comparing each left side of one tree with the right side of the other.
- Simple way
- time O(n)
room O(n) - Clever way
- time O(n)
room O(h)
The problem, simply
You get two binary trees, A and B. Say true if B is what you see when you hold A up to a mirror, and false otherwise.
In a mirror, left and right swap. So the roots must hold the same value, A's left side must be the mirror of B's right side, and A's right side must be the mirror of B's left side. Missing children must be missing on the matching side too.
A story to remember it
Picture it
Tree A on the left, tree B on the right. A's left child 2 sits on B's right side, and A's right child 3 sits on B's left side. The same happens one row lower.
Examples
A real mirror
- In
A = [1, 2, 3, 4, 5, null, 6], B = [1, 3, 2, 6, null, 5, 4]- Out
true
Every crossed pair matches: 2 with 2, 4 with 4, 5 with 5, 3 with 3, 6 with 6.
Same values, wrong side
- In
A = [1, 2, 3], B = [1, 2, 3]- Out
false
A's left child 2 must match B's right child, which is 3. They differ. These trees are equal, not mirrors.
Right values, child on the wrong side
- In
A = [1, 2, null, 3], B = [1, null, 2, 3]- Out
false
The 2s match. But A's 2 has its child 3 on the left, so B's 2 needs its 3 on the right. B's 3 is on the left, so the pair (3, nothing) fails.
A leaning chain
- In
A = [1, 2, null, 3], B = [1, null, 2, null, 3]- Out
true
A leans all the way left and B leans all the way right. Every left turn in A is a right turn in B.
Both empty
- In
A = [], B = []- Out
true
Two empty trees are mirrors of each other.
One empty
- In
A = [1], B = []- Out
false
A node can never match nothing.
Way 1: the simple way
Build the mirror of A yourself by swapping left and right at every node into a new tree. Then check if that new tree is equal to B, node by node.
mirror(node): make a copy whose left ismirror(node.right)and whose right ismirror(node.left).same(x, y): both null is true, one null is false, else values equal andsame(x.left, y.left)andsame(x.right, y.right).- Return
same(mirror(A), B).
class Solution {
public boolean areMirror(TreeNode a, TreeNode b) {
return same(mirror(a), b);
}
private TreeNode mirror(TreeNode n) {
if (n == null) return null;
TreeNode copy = new TreeNode(n.val);
copy.left = mirror(n.right); // swap sides while copying
copy.right = mirror(n.left);
return copy;
}
private boolean same(TreeNode x, TreeNode y) {
if (x == null && y == null) return true;
if (x == null || y == null) return false;
return x.val == y.val && same(x.left, y.left) && same(x.right, y.right);
}
}How long it takes: one walk to copy, one walk to compare, O(n). Extra room: a whole new tree, O(n).
Way 2: the clever way
Skip the copy. Walk both trees at the same time and compare crossed pairs directly.
- If both nodes are null, this pair is fine.
- If only one is null, it is not a mirror.
- The values must be equal.
- Check A's left with B's right, and A's right with B's left.
Compare crossed pairs, both trees at once
01 / 07
Start with the two roots. Both are 1. Equal, so keep going.
- looking now
- done
- answer
class Solution {
public boolean areMirror(TreeNode a, TreeNode b) {
if (a == null && b == null) return true; // both missing: fine
if (a == null || b == null) return false; // only one missing
return a.val == b.val
&& areMirror(a.left, b.right) // cross: left with right
&& areMirror(a.right, b.left); // cross: right with left
}
}How long it takes: each pair is checked once and we stop at the first mismatch, O(n). Extra room: only the waiting helpers, as many as the tree is tall, O(h).
Watch out for
- Checking only values, not shapes. A child missing on one side must be missing on the matching side too. The two null checks handle this.
- Writing
a == null || b == nullbeforea == null && b == null. Then two empty trees returnfalse. - Different sizes. Mirrors always have the same number of nodes, so a size check is a quick early
false, but the main walk already catches it.
What interviewers ask next
- Is one tree symmetric (a mirror of itself)? Call
areMirror(root.left, root.right). That is LeetCode 101. - Without recursion? Put the pair
(a, b)in a queue. Each step, take a pair, check it, then add(a.left, b.right)and(a.right, b.left). - Turn a tree into its mirror in place? Swap
leftandrightat every node with any walk. That is LeetCode 226, Invert Binary Tree. - What if the trees are n-ary (any number of children)? Compare child
iof A with childk - 1 - iof B.
Remember this
Grown-up words: the dancing pairs are the recursive mirror check; the pairs waiting their turn are the call stack, which is O(h) deep, where h is the height of the tree.