Skip to content

Symmetric Tree

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

A tree is symmetric if its structure mirrors across the center and all corresponding node values are equal.

TreeSymmetric?Explanation
[1, 2, 2, 3, 4, 4, 3]✓ YesMirror structure: 2 on left mirrors 2 on right, children are symmetric pairs
[1, 2, 2, null, 3, null, 3]✗ NoBoth children 3 are on the right side, not mirrored
[1]✓ YesSingle node is symmetric by definition
  • The number of nodes in the tree is in the range [1, 1000].
  • -100 <= Node.val <= 100
ApproachTimeSpaceWhen Best
DFS RecursiveO(n)O(h)General case, simple logic, or shallow trees
BFS IterativeO(n)O(w)Avoid stack overflow, wide trees, or level-wise analysis

* h = tree height; w = max width at any level

  • Pick DFS Recursive if you are comfortable with recursion and want the most concise solution.
  • Pick BFS Iterative if you prefer iterative logic or expect very deep trees.
Most Common
DFS Recursive
O(n) time · O(h) space
Iterative Alternative
BFS Iterative
O(n) time · O(w) space
★ Recommended

Check if two subtrees are mirrors of each other. For each pair of nodes at corresponding positions, verify that their values are equal and their children form mirror pairs.

The key insight is to compare the left and right subtrees as mirror images: the left’s left child should match the right’s right child, and the left’s right child should match the right’s left child.

⏱ Time O(n) Visit each node once 💾 Space O(h) Call stack depth

Input: root = [1, 2, 2, 3, 4, 4, 3]

1
/ \
2 2 <- These are mirrors if their children match in mirror fashion
/ \ / \
3 4 4 3 <- Symmetric pairs verified
StepCompareAction
1isMirror(root, root) startCompare root’s left and right subtrees
2Both nodes exist, values match (2 == 2)Check children in mirror order
3Left’s left (3) mirrors right’s right (3) ✓Left’s right (4) mirrors right’s left (4) ✓
4All leaf comparisons matchReturn true

Animated walkthrough

How the mirror comparison finds symmetry

Use Next or Autoplay to watch the algorithm compare left and right subtrees, checking that they mirror each other at each level.

Step 1 of 3 Target: symmetric
Waiting to begin...
Array state
Memory / lookup state

function isSymmetric(root):
return isMirror(root, root)
function isMirror(left, right):
if left is null and right is null:
return true
if left is null or right is null:
return false
if left.val != right.val:
return false
return isMirror(left.left, right.right) and
isMirror(left.right, right.left)
symmetric_tree_dfs.py
from typing import Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def is_symmetric_dfs(root: Optional[TreeNode]) -> bool:
"""
Check if a binary tree is symmetric using DFS recursion.
Time Complexity: O(n) - visit each node once
Space Complexity: O(h) - recursion stack, where h is height
"""
def is_mirror(left: Optional[TreeNode], right: Optional[TreeNode]) -> bool:
# Both nodes are None - symmetric
if not left and not right:
return True
# One node is None or values differ - not symmetric
if not left or not right:
return False
if left.val != right.val:
return False
# Recursively check: left's left with right's right
# and left's right with right's left (mirror pattern)
return is_mirror(left.left, right.right) and is_mirror(left.right, right.left)
return is_mirror(root, root)
# Test cases
if __name__ == "__main__":
# Example 1: Symmetric tree
# 1
# / \
# 2 2
# / \ / \
# 3 4 4 3
root1 = TreeNode(1)
root1.left = TreeNode(2)
root1.right = TreeNode(2)
root1.left.left = TreeNode(3)
root1.left.right = TreeNode(4)
root1.right.left = TreeNode(4)
root1.right.right = TreeNode(3)
print(is_symmetric_dfs(root1)) # True
# Example 2: Not symmetric
# 1
# / \
# 2 2
# \ \
# 3 3
root2 = TreeNode(1)
root2.left = TreeNode(2)
root2.right = TreeNode(2)
root2.left.right = TreeNode(3)
root2.right.right = TreeNode(3)
print(is_symmetric_dfs(root2)) # False
🎯 Interview Favourite

Use a queue to process node pairs level by level. Instead of recursion, explicitly maintain a queue of (left, right) pairs to compare, checking the mirror property iteratively.

This approach avoids recursion entirely and may perform better with very deep or unbalanced trees (reducing stack overflow risk).

⏱ Time O(n) Visit each node once 💾 Space O(w) Queue width at widest level

Input: root = [1, 2, 2, 3, 4, 4, 3]

After initialization: queue = [(root.left, root.right)] = [(node 2, node 2)]

StepQueueAction
1[(2, 2)]Values match, enqueue children in mirror order
2[(3, 3), (4, 4)]Both pairs have matching values, enqueue their children
3[] (all leaf children are null pairs)All null pairs continue, queue empties
4DoneReturn true (no mismatches found)
function isSymmetricBFS(root):
if root is null:
return true
queue = [(root.left, root.right)]
while queue is not empty:
left, right = queue.dequeue()
if left is null and right is null:
continue
if left is null or right is null:
return false
if left.val != right.val:
return false
queue.enqueue((left.left, right.right))
queue.enqueue((left.right, right.left))
return true
symmetric_tree_bfs.py
from typing import Optional
from collections import deque
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def is_symmetric_bfs(root: Optional[TreeNode]) -> bool:
"""
Check if a binary tree is symmetric using BFS with a queue.
Time Complexity: O(n) - visit each node once
Space Complexity: O(w) - queue width, where w is max nodes at any level
"""
if not root:
return True
queue = deque([(root.left, root.right)])
while queue:
left, right = queue.popleft()
# Both nodes are None - continue (symmetric so far)
if not left and not right:
continue
# One node is None or values differ - not symmetric
if not left or not right:
return False
if left.val != right.val:
return False
# Add pairs for next level: left's left with right's right
# and left's right with right's left (mirror pattern)
queue.append((left.left, right.right))
queue.append((left.right, right.left))
return True
# Test cases
if __name__ == "__main__":
# Example 1: Symmetric tree
# 1
# / \
# 2 2
# / \ / \
# 3 4 4 3
root1 = TreeNode(1)
root1.left = TreeNode(2)
root1.right = TreeNode(2)
root1.left.left = TreeNode(3)
root1.left.right = TreeNode(4)
root1.right.left = TreeNode(4)
root1.right.right = TreeNode(3)
print(is_symmetric_bfs(root1)) # True
# Example 2: Not symmetric
# 1
# / \
# 2 2
# \ \
# 3 3
root2 = TreeNode(1)
root2.left = TreeNode(2)
root2.right = TreeNode(2)
root2.left.right = TreeNode(3)
root2.right.right = TreeNode(3)
print(is_symmetric_bfs(root2)) # False
# Example 3: Single node
root3 = TreeNode(1)
print(is_symmetric_bfs(root3)) # True