Symmetric Tree: LeetCode 101 Builds on Same Tree
Symmetric Tree (#101) is one of the cleanest examples of how LeetCode problems build on each other. If you have already solved Same Tree (#100), you are ninety percent of the way there — the only twist is that you compare nodes in mirror order instead of straight order.
The problem asks a simple question: given a binary tree, check whether it is a mirror of itself. A tree is symmetric if the left subtree is a mirror reflection of the right subtree. That means every node on the left side has a matching node on the right side, at the mirrored position, with the same value.
In this walkthrough you will understand exactly what mirror comparison means, implement both recursive and iterative solutions, and see why this problem is a favorite follow-up to Same Tree in real interviews.
Understanding the Symmetric Tree Problem
You are given the root of a binary tree. Return true if the tree is symmetric around its center — meaning the left subtree is a mirror reflection of the right subtree.
Consider the tree [1,2,2,3,4,4,3]. The root is 1. Its left child is 2 and its right child is 2. The left child's left is 3 and its right is 4. The right child's left is 4 and its right is 3. Left and right subtrees are perfect mirrors, so the tree is symmetric.
Now consider [1,2,2,null,3,null,3]. The root is 1 with left child 2 and right child 2. But the left child only has a right child (3), and the right child also only has a right child (3). These are not mirrored — for symmetry, the left's right child should match the right's left child. This tree is not symmetric.
Interview Insight
Symmetric Tree (#101) is the natural follow-up to Same Tree (#100) — interviewers often ask them back-to-back to test whether you can adapt the dual-recursion pattern.
The Mirror Comparison Pattern for Symmetric Tree
The core insight is how you pair up nodes for comparison. In Same Tree, you compare left with left and right with right. In Symmetric Tree, you cross-compare: left.left with right.right, and left.right with right.left.
Think of it like folding the tree along its center axis. If you fold the left subtree onto the right subtree, every node should overlap perfectly. The outermost nodes on the left match the outermost nodes on the right. The innermost nodes on the left match the innermost nodes on the right.
This is the mirror comparison pattern. You start with root.left and root.right as a pair. Then for each pair (L, R) you check: do L and R have the same value? If yes, recurse on two new pairs: (L.left, R.right) and (L.right, R.left). If any pair fails the check, the tree is not symmetric.
- Compare left.left with right.right (outer nodes)
- Compare left.right with right.left (inner nodes)
- Both pairs must match for the tree to be symmetric
- Same values + mirror structure = symmetric tree
DFS Recursive Solution for Symmetric Tree LeetCode
The recursive approach uses a helper function isMirror(left, right) that checks whether two subtrees are mirror images. The base cases handle null nodes, and the recursive case cross-compares children.
Base case one: both left and right are null. Two empty subtrees are mirrors of each other, so return true. Base case two: exactly one of left or right is null. One subtree has a node where the other does not — they cannot be mirrors, so return false. Base case three: left.val does not equal right.val. The values differ at mirrored positions, so return false.
The recursive step is where the mirror twist happens. You call isMirror(left.left, right.right) AND isMirror(left.right, right.left). Both must return true. This is identical to Same Tree's recursion except you swap which children you pair together.
The main function simply calls isMirror(root.left, root.right). Time complexity is O(n) because you visit each node once. Space complexity is O(h) for the recursion stack, where h is the height of the tree.
- 1Define isMirror(left, right) helper function
- 2Handle base cases: both null (true), one null (false), values differ (false)
- 3Recurse: return isMirror(left.left, right.right) AND isMirror(left.right, right.left)
- 4Call isMirror(root.left, root.right) from the main function
Pro Tip
The key difference from Same Tree: instead of comparing left-left and right-right, compare left.left with right.right AND left.right with right.left. Cross-compare, don't straight-compare.
BFS Iterative Solution for Symmetric Tree
The iterative approach uses a queue to process node pairs level by level. Instead of recursion, you explicitly enqueue the pairs that need to be compared in mirror order.
Initialize a queue with the pair (root.left, root.right). While the queue is not empty, dequeue a pair. If both are null, continue to the next pair. If one is null or their values differ, return false. Otherwise, enqueue two new pairs: (left.left, right.right) and (left.right, right.left).
This is the same logic as the recursive solution, just expressed with a queue instead of the call stack. The BFS approach can be useful in interviews when the interviewer asks for a non-recursive solution or when you want to avoid stack overflow on deeply unbalanced trees.
Time complexity remains O(n) and space complexity is O(n) in the worst case for the queue — which is actually worse than the recursive O(h) space for balanced trees, but avoids stack overflow risks.
Edge Cases for Symmetric Tree
Edge cases are straightforward for this problem, but interviewers like to ask about them. An empty tree (root is null) is symmetric — there is nothing to be asymmetric. A single node with no children is symmetric — it is its own mirror.
A tree with two nodes (root plus one child) is never symmetric. If the root has only a left child, the right side is empty, so the tree is not a mirror. Same if it has only a right child.
Watch out for the subtle case where values match but structure does not. The tree [1,2,2,null,3,null,3] has matching values at each level, but the null positions are not mirrored. Your mirror comparison catches this naturally because you compare left.right with right.left, not left.right with right.right.
- Empty tree (null root): return true — symmetric by definition
- Single node: return true — mirrors itself
- Two nodes: always false — one side is empty
- Matching values but wrong structure: caught by mirror pairing
Watch Out
Don't try to check symmetry with inorder traversal — a symmetric tree's inorder traversal is palindromic, but palindromic inorder doesn't guarantee symmetry. The recursive mirror check is cleaner.
What Symmetric Tree Teaches You
Symmetric Tree is a masterclass in adapting a known pattern. If you know Same Tree, you know Symmetric Tree — the only change is which children you pair for comparison. This is exactly the kind of pattern adaptation that interviewers test.
The mirror comparison pattern shows up in other problems too. Whenever you need to check if a structure is a palindrome or reflection, the same cross-comparison idea applies. Think of it as the tree equivalent of checking a string from both ends toward the middle.
Practice recognizing when a new problem is really a slight twist on a problem you already know. Symmetric Tree builds on Same Tree. Subtree of Another Tree builds on Same Tree in a different way. These connections are what make pattern-based study with YeetCode so effective — you learn the core pattern once and apply it across a family of problems.