Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Check if a tree is symmetric

Determine whether a binary tree is a mirror of itself (symmetric around its center).

Python practice15 minTrees & Binary TreesIntermediateLast updated March 29, 2026

Problem statement

Given the root of a binary tree, determine if it is symmetric around its center. A binary tree is symmetric if the left subtree is a mirror reflection of the right subtree. Return True if the tree is symmetric, otherwise return False. You may use a recursive approach (comparing pairs of nodes) or an iterative approach with a queue/stack.

Task

Implement a function that checks whether a binary tree is symmetric (mirror) using recursion or iteration.

Examples

Symmetric example

Input

root = TreeNode(1, TreeNode(2, TreeNode(3), TreeNode(4)), TreeNode(2, TreeNode(4), TreeNode(3)))

Output

True

Left and right subtrees are mirrors of each other.

Input format

A binary tree constructed using TreeNode(val, left, right). The root may be None.

Output format

Return True if the tree is symmetric, otherwise False.

Constraints

- Number of nodes can be zero (empty tree) or more. - Aim for O(n) time and O(h) extra space (recursion stack) or O(n) with iterative approach.

Samples

Sample 1

Input

TreeNode(1, TreeNode(2, None, TreeNode(3)), TreeNode(2, None, TreeNode(3)))

Output

False

Structure not mirrored even though some values match.