We need to determine if a binary tree is a mirror image of itself. Fold the tree along the vertical line through the root. If the left half overlaps the right half, the tree is symmetric.
The mirror condition is defined on the two subtrees of the root: the left subtree and the right subtree must be reflections of each other. For two subtrees to be mirrors:
This differs from checking whether the two subtrees are identical. Identical means structurally the same. Mirror means one is the flipped version of the other. So we compare the left child of the left subtree with the right child of the right subtree, and the right child of the left subtree with the left child of the right subtree.
Number of nodes in [1, 1000] → The tree is small, but since we must inspect every node at least once to confirm symmetry, O(n) is the target.-100 <= Node.val <= 100 → Node values are small integers, so equality comparison carries no overflow risk.At least 1 node → The tree is never empty. The null-root check below is defensive rather than required by the constraints.The mirror condition is recursive. A tree is symmetric if its left subtree is a mirror of its right subtree, and two subtrees are mirrors of each other if:
This maps directly to a helper isMirror(left, right) that checks whether two subtrees are mirrors. We call it once with isMirror(root.left, root.right), and conditions 2 and 3 generate the recursive calls.
The base cases close the recursion. If both nodes are null, they are mirrors (two empty subtrees are reflections of each other). If exactly one is null, the structures differ, so they cannot be mirrors. If their values differ, they are not mirrors. Otherwise, we recurse on the cross-paired children.
isMirror(root.left, root.right).isMirror(left, right):isMirror(left.left, right.right) AND isMirror(left.right, right.left).The recursion is optimal in time but relies on the call stack, which can overflow on a deeply skewed tree. The next approach replaces the call stack with an explicit queue.
This approach runs the same comparison as the recursion, but manages the pending work in a queue instead of the call stack.
We enqueue the root's left and right children as a pair. Then we process pairs one at a time: dequeue two nodes, check whether they form a valid mirror at this position, and if so, enqueue their children in mirror order. A mismatch returns false immediately. If every pair matches, the tree is symmetric.
Correctness rests entirely on the enqueue order. Each enqueued pair represents two positions that must hold equal values for the tree to be symmetric. By pairing the outer children together (left's left with right's right) and the inner children together (left's right with right's left), every position the mirror condition requires to be equal is enqueued exactly once, so no required comparison is skipped. Pairing children in their natural order instead would test structural equality, which is a different property.
This trace uses the asymmetric tree [1, 2, 2, null, 3, null, 3] to show how the queue detects a mismatch and stops early.