Symmetric Tree

easy

Given the root of a binary tree, return true if it is symmetric — a mirror image of itself around its center — and false otherwise.

The tree is given in level-order (breadth-first), using null for missing children.

Hints

A tree is symmetric when its left subtree is the mirror image of its right subtree.
Two subtrees mirror when their roots are equal and the outer and inner child pairs mirror.
It's the same-tree comparison, but with the recursion crossed: a.left vs b.right, a.right vs b.left.

Common doubts

Only the recursion crosses the children: mirror(a.left, b.right) and mirror(a.right, b.left) instead of (left,left) and (right,right).
A mirror flips left and right, so a node's left child must match the mirrored node's right child (outer), and vice versa (inner).
Yes — it trivially mirrors itself, as does the empty tree.

Interview follow-ups

Swap every node's left and right children recursively (invert binary tree).
Yes — a level-order or preorder serialization with null markers should read as a palindrome per level, though the crossed recursion is cleaner.

Fun facts

  • Symmetric-tree is the same-tree algorithm with the child comparisons crossed.
  • Inverting a tree and then running the same-tree check against the original is an equivalent (if wasteful) way to test symmetry.

Asked at

AmazonMicrosoftFacebook
Frequently Sometimes Occasionally
Example 1
Input: root = [1, 2, 2, 3, 4, 4, 3]
Output: true
The left and right subtrees are mirror images of each other.
Example 2
Input: root = [1, 2, 2, null, 3, null, 3]
Output: false
The 3s are both on the inner side rather than mirrored, breaking symmetry.
Constraints

- The number of nodes is in the range [1, 1000] - -100 <= Node.val <= 100

Solve this problem →