Symmetric Tree

Easy

Topics
TreeDFSBFS

Given the root of a binary tree, check whether it is a mirror of itself (symmetric around its center).

Example 1

3241423
The tree equals its own mirror image, so the answer is true. The recursion pairs left.left with right.right and left.right with right.left — outside with outside, inside with inside.
Tree given in level-order (breadth-first); null marks a missing child.
Input:  root = [1,2,2,3,4,4,3]
Output: true

Example 2

23123
breaks the mirror
The shaded nodes break the mirror, so the answer is false. Note what is compared: the left subtree against the reversed right subtree, which is not the same as checking each subtree for symmetry on its own.
Input:  root = [1,2,2,null,3,null,3]
Output: false

Constraints

  • 1 <= number of nodes <= 1000
  • -100 <= Node.val <= 100
Editorial — intuition, approach, complexity and pitfalls

Intuition

Symmetry is a property of two subtrees being mirror images: the left subtree’s left child mirrors the right subtree’s right child, and vice versa.

Approach

  1. Compare two nodes at a time, starting with (root.left, root.right).
  2. Two mirror nodes must have equal values, and (a.left, b.right) and (a.right, b.left) must also mirror.
  3. Both null → mirror; one null → not mirror.

Dry Run

tree 1 2 2 3 4 4 3
  mirror(2, 2)       equal, so compare (a.left, b.right) and (a.right, b.left)
    mirror(3, 3)     equal
      both null      -> true
      both null      -> true
    mirror(4, 4)     equal
      both null      -> true
      both null      -> true
  -> true
tree 1 2 2 null 3 null 3
  mirror(2, 2)       equal, so compare (a.left, b.right) and (a.right, b.left)
    mirror(null, 3)  one side null  -> false
  -> false
The second tree is the one that explains the pairing. Both 2s have a right child
of 3 and no left child, so the tree looks "the same on both sides" if you compare
left-with-left. It is not symmetric: a mirror reflects, so the left node's LEFT
must match the right node's RIGHT. mirror(null, 3) catches it immediately.
Note the function takes two nodes, not one. Symmetry is not a property a single
node can report on, which is why the public method starts the recursion at
(root.left, root.right) rather than at the root, and why a null root is true by
itself without entering the helper at all.
The `not a and not b` test has to come first. Let `not a or not b` run earlier
and a pair of absent children reads as asymmetric, which poisons everything: the
symmetric tree above returns false, and so does the single node "1", because its
two null children are the first pair compared.

Complexity

  • Time — O(n): each node is compared once.
  • Space — O(h): recursion stack.

Common Pitfalls

  • Mirror the outer and inner pairs correctly: left.left vs right.right, and left.right vs right.left.

Full Solution — all languages

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def isSymmetric(self, root):
        def mirror(a, b):
            if not a and not b:
                return True
            if not a or not b or a.val != b.val:
                return False
            return mirror(a.left, b.right) and mirror(a.right, b.left)
        return mirror(root.left, root.right) if root else True
Asked at LinkedIn
SensAI
Beta
Tap the mic to stop voice mode