Problem Statement
Symmetric Tree
You are given the root of a binary tree. A binary tree is a structure where each item, called a node, holds a value and can point to a left child and a right child. The "root" is the top node where the tree begins. Your job is to check if the tree is symmetric, which means the left side is a mirror image of the right side. Picture folding the tree down the middle: if the two halves line up exactly, it is symmetric.
Signals to notice
Brute force first
Generate both subtrees as arrays and compare — but wasteful. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.
The key insight
Recursive comparison: left.left matches right.right, and left.right matches right.left. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.
Trace it on root=[1,2,2,3,4,4,3]
isMirror(L=2, R=2): both non-null, 2==2 -> recurse on outer (L.left, R.right) and inner (L.right, R.left) outer isMirror(L=3, R=3): both non-null, 3==3 -> recurse children isMirror(null, null)=true (both); isMirror(null, null)=true -> outer returns true inner isMirror(L=4, R=4): both non-null, 4==4 -> recurse children isMirror(null, null)=true (both); isMirror(null, null)=true -> inner returns true top frame: outer(true) AND inner(true) = true return true
What must stay true
Two subtrees are mirrors if outer children match and inner children match. If that remains true after every update, the rest of the reasoning has a stable place to stand.
Shape of the loop
function isMirror(left, right):
if left is null and right is null: return true
if left or right is null, or left.val != right.val: return false
return isMirror(left.left, right.right)
AND isMirror(left.right, right.left)
return isMirror(root.left, root.right)Pseudocode only — the full worked solution lives in the Solution tab.
Easy way to go wrong
Only checking values without checking structure — both values AND structure must mirror. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.