Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
root = [1,2,2,3,4,4,3]trueroot = [1,2,2,null,3,null,3]falseisMirror(root.left, root.right)true; one null → falseL.val == R.val — mismatch → falseisMirror(L.left, R.right) AND isMirror(L.right, R.left)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.
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.
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.
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.