← Back to DSA Animator
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.