Symmetric Tree
Symmetric Tree is LeetCode 101 (Easy). Given the root of a binary tree, return true if the tree is a mirror of itself around its centre, and false otherwise.
- The two halves must match in shape as well as in values.
- The tree has 1 to 1,000 nodes with values from -100 to 100.
- The follow-up asks for both a recursive and an iterative solution.
- The number of nodes in the tree is in the range [1, 1000].
- -100 <= Node.val <= 100
Intuition
Fold the tree down the middle. It is symmetric when every node on the left half lands on a node with the same value on the right half, so the real question is about two nodes at a time: a left node and the node in its mirrored spot.
- Mirrored spots cross: the partner of a left child is a right child. Below a matching pair, the two outer children pair up, and so do the two inner ones.
- Each pair is settled on its own: two empty spots match, one empty spot breaks the shape, and two nodes need equal values.
Start with the root's two children. The tree is symmetric only if every pair passes.
When a question compares a tree with its own reflection, write a helper that takes two nodes and recurse with the children crossed.
Approach
Before reading on, take the root's two children as a pair. Decide when that pair passes, and which two pairs of grandchildren you must check next. Aim for one visit per node.
Two ways to solve it
A helper compares one mirrored pair and calls itself on the outer and inner pairs.
- Short: four lines in the helper.
- Early exit:
andstops at the first bad pair.
The usual interview answer.
Keep the pairs still to check in a queue and handle one pair per loop.
- No recursion: nothing to overflow.
- Answers the follow-up: the problem asks for an iterative version too.
The queue can hold a whole level of pairs.
Both read every node once. The recursive helper is shorter, so the steps, code and live demo use it; the queue version follows the demo.
Start from the root's two children
Call mirror(root.left, root.right). The root sits on the fold line, so it has no partner; only its children have to reflect each other. A one-node tree passes at once, because both children are missing.
Settle missing nodes before reading values
If both nodes are null, return true: two empty spots mirror each other. If exactly one is null, return false: one side has a node where the other has none. Both checks run before .val is read, so a null is never dereferenced.
Compare the two values
With both nodes present, return false when a.val != b.val. The two nodes sit in mirrored spots, so a symmetric tree must hold the same value in both of them.
Check the outer pair, then the inner pair
Return mirror(a.left, b.right) and mirror(a.right, b.left). The children cross because a reflection swaps left and right. The and skips the inner pair when the outer pair already failed, so the first mismatch anywhere ends the walk.
Code in Python, C++ and Java
and in the return line stops there, so the pairs still waiting are never checked and the answer is false.and in the return line stops there, so nothing else is checked and the answer is false.Queue of pairs
The queue holds pairs of nodes that must mirror each other, starting with the root's two children. Each loop takes one pair and applies the same three checks. A passing pair adds its outer pair and its inner pair to the queue. In Java, use LinkedList here: ArrayDeque throws on null, and missing children are pushed as nulls.
Common pitfalls
Pairing left with left
return mirror(a.left, b.left) and mirror(a.right, b.right)
return mirror(a.left, b.right) and mirror(a.right, b.left)
Uncrossed calls test whether the two halves are copies, not reflections. On [1,2,2,3,4,4,3], which is symmetric, the left half's 3 gets compared with the right half's 4, so the code returns false.
Checking that a traversal reads the same both ways
vals = inorder(root) return vals == vals[::-1]
return mirror(root.left, root.right)
A list of values forgets where the empty spots are. [1,2,2,2,null,2] has inorder 2, 2, 1, 2, 2, a palindrome, yet both lower 2s hang on the left. Checking each level for a palindrome while skipping nulls fails the same way on [1,2,2,null,3,null,3].
Edge cases
[1,2]The first pair is 2 against an empty spot, so the one-null check returns false before any value is compared.
[1,2,2,null,3,null,3]The pair of 2s passes, but the outer pair below them is an empty spot against a 3, so the answer is false. Values alone never catch this.
Complexity
Symmetric Tree FAQ
How is this different from Same Tree?
Same Tree walks two separate trees and pairs left with left. Here there is one tree, and each node is paired with its reflection, so the left child of one node meets the right child of the other.
Can I mirror the tree and then compare it with the original?
Yes. Invert a copy of the tree and run the Same Tree check on the two. It is correct, but it builds a second tree with O(n) extra memory, while the pair check does the same comparison without building anything.
Is a symmetric tree also a complete one?
Not necessarily; the two ideas are unrelated. [1,2,2,null,3,3] is symmetric but not complete, and [1,2,3] is complete but not symmetric. Symmetry only asks that mirrored spots agree.