DSA AnimatorDSA animations
Symmetric Tree LC #101 Easy DFS · Mirror Check
Problem

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

Example 1
Input: root = [1,2,2,3,4,4,3]
Output: true
Explanation: The tree is symmetric — left subtree mirrors the right subtree.
Example 2
Input: root = [1,2,2,null,3,null,3]
Output: false
Explanation: The right children do not mirror the left children.
Constraints: 1 ≤ nodes ≤ 1000  |  −100 ≤ Node.val ≤ 100
Try Examples
Custom:
Approach
DFS Recursive Mirror Check
Check if left subtree mirrors right subtree. At each level: compare L.val == R.val, then recurse L.left ↔ R.right and L.right ↔ R.left (mirror positions).
Mirror Comparison
L — left node
—
R — right node
—
Waiting for comparison…
Binary Tree
L node (blue)
R node (orange)
Match ✓
Mismatch ✗
Visited
Variables
L.val
—
R.val
—
match?
—
depth
—
Call Stack
Step Logic
Press ▶ Play or Next Step to begin the animation.
✓
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Call isMirror(root.left, root.right)
2
Both null → true; one null → false
3
Compare L.val == R.val — mismatch → false
4
Recurse: isMirror(L.left, R.right) AND isMirror(L.right, R.left)
Time
O(n)
Space
O(h)
Why Mirror Recursion Works

A tree is symmetric when its left and right subtrees are mirrors of each other. Two subtrees mirror each other when their roots have equal values AND the left child of L mirrors the right child of R (and vice versa). The recursion cross-checks these mirror positions at every depth simultaneously. Both null means the branch is balanced; one null is an asymmetry; differing values short-circuit immediately, pruning all unnecessary deeper checks.

How to solve Symmetric Tree

LeetCode 101 · Easy · Tree · Pattern: Mirror Recursion

Why the obvious approach is slow

You could collect each level and check whether it reads the same both ways. But you must also record missing children, which makes it messy. A recursive mirror check is simpler.

The key idea

Write a helper that compares two nodes, a and b, that should mirror each other. If both are null, they match. If only one is null, or their values differ, they do not match. Otherwise, the outside pair must match, which is a left with b right. And the inside pair must match, which is a right with b left.

Remember

Compare outside with outside, and inside with inside. Pass two nodes into the recursion, not one.

Run the animation above on your own input, or watch the 1:58 video explanation of Symmetric Tree with a dry run and Java code.

More Tree problems