EasyDepth-First SearchBreadth-First SearchTreeUpdated Sep 2026

101. Symmetric Tree

Asked at Google, Meta, Amazon, Microsoft and 1 more

Difficulty

Easy

Time

O(n)

Space

O(h)

Pattern

Depth-First Search

Companies

5

Problem

You are given the root of a binary tree. Decide whether the tree looks like a mirror image of itself when you draw it and fold it along a vertical line through the root.

In other words, the left subtree and the right subtree must be reflections of each other. This is not the same as the two subtrees being identical. When you compare two nodes as mirrors, the left child of one must match the right child of the other, and the right child of one must match the left child of the other.

Return true if the tree is symmetric, and false otherwise. An empty tree counts as symmetric.

Mirror, not copy

32left side4142right side3Outer pair (3,3) and inner pair (4,4) both match, so the answer istrue.
Folding the tree along the root line, node 3 on the left must line up with node 3 on the right, and 4 with 4.

Constraints

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

Examples

Example 1
Inputroot = [1,2,2,3,4,4,3]
Outputtrue
WhyThe left subtree is 2 with children 3 and 4. The right subtree is 2 with children 4 and 3. Reading the right side backwards gives 3, 4 — the same as the left side. So the two halves are reflections.
Example 2
Inputroot = [1,2,2,null,3,null,3]
Outputfalse
WhyThe left child of the left 2 is missing, but its right child is 3. On the other side, the left child of the right 2 is 3 and its right child is missing. The outer pair is (null, 3) and the inner pair is (3, null) — neither matches, so the tree is not a mirror.

Asked At

5 companies

How to Think About It

1

The tree is symmetric if the left subtree is a mirror of the right subtree. Compare them as mirrors, not as identical trees.

2

Mirror comparison: left.left vs right.right (outer nodes), and left.right vs right.left (inner nodes). This is different from same-tree comparison.

3

Recursive helper: mirror(l, r) checks if two nodes are mirrors. Both null → true. One null → false. Values differ → false. Otherwise recurse: mirror(l.left, r.right) AND mirror(l.right, r.left).

4

Edge cases: empty tree (symmetric), single node (symmetric), one child each side (check values).

Optimal Approach

Step 1: If root is null, return true.
Step 2: Call helper mirror(root.left, root.right).
Step 3: mirror(l, r):

  • Both null → true
  • One null → false
  • Values differ → false
  • Return mirror(l.left, r.right) AND mirror(l.right, r.left)

The key difference from same-tree: you compare l.left with r.right (outer) and l.right with r.left (inner). This is mirror comparison, not direct comparison.

Time: O(n). Space: O(h).

Compare the two subtrees as mirrors

32mirror(2, 2)4142values equal3Step 1 — the two children of the root must have the same value.3outer: 3 vs 324inner: 4 vs 414inner: 4 vs 423outer: 3 vs 3Step 2 — compare outer children together and inner children together.mirror(l, r) = l.val == r.val and mirror(l.left, r.right) and mirror(l.right, r.left)
Each call pairs one node from the left side with one node from the right side, always crossing over.
ComplexityTime O(n)Space O(h)Every node is checked once; h is the tree height, which is the depth of the recursion stack.

What Trips People Up in Real Interviews

1

Confusing this with "same tree." Symmetric means the tree is a mirror of itself, not that two trees are identical. Compare left with right as mirrors.

2

Getting the mirror comparison wrong. The outer nodes are left.left vs right.right. The inner nodes are left.right vs right.left. This is different from same-tree comparison.

3

Not handling the base cases: both null (true), one null (false), values differ (false).

4

Forgetting that a single node tree is symmetric. Return true.

5

Trying to solve it by inverting one subtree and then comparing with same-tree. While technically correct, it's O(n) extra work. Direct mirror comparison (l.left vs r.right) is cleaner.

Solution Code

O(n) time
def isSymmetric(root):
    def mirror(l, r):
        if not l and not r:
            return True
        if not l or not r:
            return False
        return l.val == r.val and mirror(l.left, r.right) and mirror(l.right, r.left)
    return mirror(root.left, root.right) if root else True

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Symmetric Tree problem?

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

How do you solve Symmetric Tree?

The optimal approach runs in O(n) time and O(h) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Symmetric Tree?

Symmetric Tree is asked at Google, Meta, Amazon, Microsoft, Apple. It is a easy difficulty problem.

What are common mistakes on Symmetric Tree?
  • Confusing this with "same tree." Symmetric means the tree is a mirror of itself, not that two trees are identical. Compare left with right as mirrors.
  • Getting the mirror comparison wrong. The outer nodes are `left.left` vs `right.right`. The inner nodes are `left.right` vs `right.left`. This is different from same-tree comparison.
  • Not handling the base cases: both `null` (`true`), one `null` (`false`), values differ (`false`).
  • Forgetting that a single node tree is symmetric. Return `true`.
  • Trying to solve it by inverting one subtree and then comparing with same-tree. While technically correct, it's `O(n)` extra work. Direct mirror comparison (l.left vs r.right) is cleaner.