DSA Tracker

Problem

Easy

Symmetric Tree

An easy Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Binary Trees
Sheets
3
Core for
4 roles

The problem

Fold a binary tree down its middle: is the left half an exact mirror image of the right half? Return true if it is, false otherwise.

Example 1

Input
[7,4,4,2,9,9,2]
Output
true
Why
Left and right subtrees mirror each other: 4 and 4, then 2,9 against 9,2.

Example 2

Input
[5,3,7,1,4,6,null]
Output
false
Why
Left has 6 on right, right has no corresponding node - not mirrors.

Example 3

Input
[1]
Output
true
Why
A single node is always symmetric.

Constraints

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

How to think about it

Updated 2026-09-29

Symmetry is not an operation on one tree; it is an equality comparison between two trees moving in opposite directions. The left child of the left branch must match the right child of the right branch, and the inner child of the left must match the inner child of the right.

Approaches, worst first

  1. Iterative paired queue

    time O(n) · space O(n)

    Enqueue root's left and right children. Each iteration pops two nodes and verifies values. Then enqueue outer children together and inner children together. Avoids call stack recursion at the cost of queue overhead.

  2. Dual recursive mirror checkWrite this one

    time O(n) · space O(h)

    Write a helper `isMirror(t1, t2)`. Both null returns true; exactly one null or value mismatch returns false. Recurse simultaneously on `(t1.left, t2.right)` and `(t1.right, t2.left)`. Minimal code with immediate early exit.

Where people lose marks · 3
  • Comparing `t1.left` with `t2.left` instead of `t2.right`; standard equality checks identity rather than mirror reflection.
  • Assuming symmetric inorder traversal guarantees a symmetric tree; a non-symmetric tree like [1,2,2,2,null,2] can share the same inorder values.
  • Dereferencing `.left` or `.right` on a null pointer when one child is absent.

The theory behind it

Binary Trees — the ground this problem stands on. All Binary Trees problems

What Binary Trees is

A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.

When to reach for it

Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.

How the pattern works

Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.

More Binary Trees problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .