Symmetric Tree (LeetCode 101): Solution and Explanation

Easy
Tree
Depth First Search
Breadth First Search
Binary Tree

Decide whether a binary tree is a mirror image of itself around its center, meaning the left subtree is the reflection of the right subtree in both structure and values.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

One brute force idea is to produce a level-order listing of the tree with placeholders for missing nodes and check that each level reads the same forwards and backwards. That works, but it needs extra memory for every level and careful handling of the placeholders. A cleaner idea comes from asking what mirror actually means for two subtrees.

Two subtrees are mirrors of each other when their roots have the same value, the left child of one mirrors the right child of the other, and the right child of one mirrors the left child of the other. That definition is recursive, so it turns directly into a recursive function that takes two nodes at once instead of one.

The base cases handle missing nodes. If both nodes are None, that position is empty on both sides, which is symmetric. If exactly one is None, the shapes differ and the tree cannot be symmetric. Otherwise compare values and recurse on the two crossed pairs: the outer pair (left.left with right.right) and the inner pair (left.right with right.left).

The whole tree is symmetric when the root's left and right subtrees mirror each other, so the reference code starts with dfs(root.left, root.right). Because and short-circuits, the recursion stops as soon as a mismatch is found. That means an obviously lopsided tree is rejected after only a few comparisons, while a truly symmetric tree requires visiting every node once, which is the best you can do since any unchecked node could break the mirror.

Approach: Recursive mirror comparison

  1. If root is None, return true.
  2. Define dfs(left, right) that checks whether two subtrees are mirrors.
  3. If both left and right are None, return true. If only one of them is None, return false.
  4. Return true only if left.val == right.val, dfs(left.left, right.right) is true and dfs(left.right, right.left) is true.
  5. Return dfs(root.left, root.right).

Step-by-step example

root = [1,2,2,3,4,4,3]
  1. Start with dfs(2, 2) on the root's two children. The values match.
  2. Outer pair: dfs(3, 3) compares the left child's left with the right child's right. The values match, and both have only None children, so it returns true.
  3. Inner pair: dfs(4, 4) compares the left child's right with the right child's left. The values match and the children are all None, so it returns true.
  4. Both crossed comparisons succeed, so dfs(2, 2) returns true and the tree is symmetric.
  5. If the input were [1,2,2,null,3,null,3], the inner pair would compare 3 with None and return false.

Code: Python, Java, C++

1class Solution:
2    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
3        if not root:
4            return True
5            
6        def dfs(left, right):
7            if not left and not right:
8                return True
9            if not left or not right:
10                return False
11            
12            return (left.val == right.val and 
13                   dfs(left.left, right.right) and
14                   dfs(left.right, right.left))
15                   
16        return dfs(root.left, root.right)

Time and space complexity

Time

O(n)

Each node is compared at most once as part of one mirrored pair, so the work is linear in the number of nodes.

Space

O(h)

The recursion depth equals the tree height h, which is O(n) in the worst case.

Other approaches

Iterative with a queue of pairs (time O(n), space O(n))

Push the pair (root.left, root.right) onto a queue. Repeatedly pop a pair, apply the same None and value checks, then push the outer pair and the inner pair. It avoids recursion depth issues on very deep trees.

Mirror and compare (time O(n), space O(n))

Make an inverted copy of the right subtree and check whether it is the same tree as the left subtree. It reuses familiar helpers but allocates a copy and does two passes.

Common mistakes

  • Comparing left.left with right.left. That checks whether the subtrees are identical, not mirrored.
  • Checking only that each level's values form a palindrome while ignoring missing nodes, which wrongly accepts trees whose shapes differ.
  • Accessing .val before the None checks, which crashes when one side is missing.
  • Starting the recursion with dfs(root, root). It works but compares every pair twice, so starting with the two children is cleaner.

Frequently asked questions

How do you check if a binary tree is symmetric?

Compare the left and right subtrees as mirrors. Two nodes mirror each other when their values match, the left child of one mirrors the right child of the other, and vice versa.

Can Symmetric Tree be solved iteratively?

Yes. Use a queue or stack of node pairs, starting with the root's two children, and push the outer and inner pairs after each successful comparison.

What is the time complexity?

It is O(n) time, since each node is part of exactly one comparison. Space is O(h) for recursion or O(n) for the iterative queue.

How is this different from Same Tree?

Same Tree compares left with left and right with right. Symmetric Tree crosses them, comparing left with right and right with left.