Symmetric Tree solution explained

A binary tree is symmetric when its left subtree is a mirror image of its right subtree. Check it by comparing the two subtrees in pairs: the values must match, the left child of one must mirror the right child of the other, and the right child of one must mirror the left child of the other. One recursive helper or a queue of node pairs does it in O(n) time.

By Published 7 min read

The problem, reworded

Given the root of a binary tree, return true if the tree is its own mirror image. Picture a vertical line through the root: every node on the left must have a node with the same value at the mirrored position on the right, and every gap on one side must be matched by a gap on the other. An empty tree and a single node are both symmetric.

         5                             5
       /   \                         /   \
      7     7                       7     7
     /       \                     /       \
    1         1                   1         1
     \       /                     \         \
      4     4                       4         4

    symmetric                    not symmetric
Two trees drawn for this page. Same values, different answers.

The right tree fails even though each level holds matching values. On the left side the 4 hangs to the right of its parent 1, and on the right side it also hangs to the right, where a mirror image would need it on the left. Shape counts, not only values.

Symmetric Tree is short, and it separates candidates who can define recursion over two trees at once from those who reach for a level-by-level list and then patch it. It also tests whether you can say exactly which children to compare, a detail that is easy to get backward under pressure. Interviewers often ask for both a recursive and an iterative answer.

The shortcut that breaks

One idea is to collect each level's values and check that each list reads the same forward and backward. On the right-hand tree, the levels are [5], [7, 7], [1, 1] and [4, 4], all palindromes, yet the tree is not symmetric. You can rescue the idea by recording a marker for every missing child, but then you are storing a whole level at a time and handling markers. Comparing nodes directly is simpler and stops at the first mismatch.

Recursive mirror check

Write a helper that takes two nodes, a and b, and answers whether the subtree at a is the mirror of the subtree at b. If both are empty, yes. If exactly one is empty, no. Otherwise the values must be equal, a's left must mirror b's right (the outer pair) and a's right must mirror b's left (the inner pair). The whole tree is symmetric when the root's left and right subtrees mirror each other.

def is_mirror_image(root):
    def mirrored(a, b):
        if a is None or b is None:
            return a is b
        return (a.val == b.val
                and mirrored(a.left, b.right)
                and mirrored(a.right, b.left))
    return root is None or mirrored(root.left, root.right)
Python: one helper over pairs of nodes. O(n) time, O(h) recursion depth.

Compare it with checking whether two trees are identical: the code is the same except that the children are crossed. If you have already practiced Same Tree, say so and point at the one line that changes.

Iterative check with a queue of pairs

Replace recursion with a list of pairs still to compare. Start with the root's two children. Take a pair: two empties are fine, one empty or unequal values mean false, and otherwise add the outer pair and the inner pair. If the list runs out, every mirrored position matched.

function isMirrorImage(root) {
  if (root === null) return true;
  const queue = [[root.left, root.right]];
  for (let k = 0; k < queue.length; k++) {
    const [a, b] = queue[k];
    if (a === null && b === null) continue;
    if (a === null || b === null || a.val !== b.val) return false;
    queue.push([a.left, b.right], [a.right, b.left]); // outer pair, inner pair
  }
  return true;
}
JavaScript: a queue of node pairs, walked with a moving index instead of shift().

Reading the queue with an index avoids the cost of removing items from the front of a JavaScript array. A stack works just as well, since the order in which pairs are checked does not change the answer.

Trace on the left-hand tree

Start with the pair (7, 7). The values match, so add the outer pair (1, 1) and the inner pair of two empty children, which is skipped. The 1s match, so add their outer pair, the left 1's empty left child with the right 1's empty right child, and their inner pair, (4, 4). Everything matches and the queue runs out, so the tree is symmetric. On the right-hand tree, the walk gets as far as the two 1s, and then the outer pair compares the left 1's empty left child with the right 1's right child, 4. One side is empty and the other is not, so the answer is false.

Complexity

n nodes, height h
ApproachTimeExtra space
Per-level palindrome with gap markersO(n)O(width of the widest level)
Recursive mirror helperO(n)O(h) call stack
Queue or stack of pairsO(n)O(n) in the worst case
n nodes, height h. Scroll sideways on a small screen.

Edge cases

  • An empty tree: symmetric.
  • A root with no children: symmetric.
  • A root with only one child: not symmetric, since the other side is empty.
  • Mirrored shape but one different value deep down: the check must reach that pair, which it does, and returns false there.
  • Negative or repeated values: only equality is used, so nothing special is needed.

Mistakes that come up

  • Comparing a.left with b.left and a.right with b.right, which checks sameness, not mirroring.
  • Calling the helper on (root, root) and then forgetting that it does twice the work. It is correct but redundant; starting from the two children is cleaner.
  • Checking values level by level without gap markers, as in the right-hand tree.
  • Dereferencing a child before checking whether the node exists.

Follow-up questions

  • Invert the tree instead. A separate problem: swap the children of every node to produce the mirror image. A tree is symmetric exactly when inverting it leaves it unchanged, which makes a handy test reference.
  • Are two separate trees mirror images of each other? Call the helper on the two roots directly.
  • An n-ary tree. Compare the first child of one node with the last child of the other, the second with the second-to-last, and so on.
  • Which approach would you ship? For very deep trees in Python, the iterative version avoids the recursion limit; otherwise the recursive one is clearer.

How to say it

Fictional example

One way to explain the solution
Candidate
Symmetric means the left subtree mirrors the right one, so I'll compare nodes two at a time instead of whole levels, because levels can hide shape differences.
Candidate
Two empty nodes match, one empty doesn't, and otherwise the values must be equal and the children cross over: outer with outer, inner with inner.
Candidate
It visits each node once, so linear time, with recursion depth equal to the height. I can do the same thing with a queue of pairs if depth is a concern.

Same Tree is the uncrossed version of this check. Path Sum is another short recursion where the base case at the leaves decides correctness. Binary Tree Inorder Traversal shows how to turn tree recursion into an explicit stack, as the queue does here. The coding problems hub lists them all.

Practicing it with Natively

During practice on a Mac or a Windows PC, Natively can read a tree prompt from a screenshot, and you can ask it which children should be paired or for a tree shaped like the right-hand example to test your code. It keeps a transcript and notes of your mock, so you can see how clearly you explained the outer and inner pairs. See the AI interview assistant page.

Questions people ask

How do you check if a binary tree is symmetric?

Compare the root's left and right subtrees as mirror images. Two empty nodes match, one empty does not, and otherwise the values must be equal, the outer children must mirror each other and the inner children must mirror each other.

Can I check Symmetric Tree level by level?

Only if you record a marker for every missing child. Without markers, a level can read the same both ways while the shape is not mirrored, such as two right children with equal values.

What is the complexity of Symmetric Tree?

O(n) time, since each node is compared once. The recursive version uses O(h) stack space; a queue of pairs can hold O(n) pairs in the worst case.

How is Symmetric Tree related to Same Tree?

It is the same two-tree comparison with the children crossed: left with right and right with left, instead of left with left.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP