Symmetric Tree - Problem
Mirror Tree Checker: Imagine looking at a binary tree and asking, "Is this tree a perfect reflection of itself?"
You're given the
A symmetric tree has the property that if you draw a vertical line through the root, the left side is exactly the same as the right side, but flipped horizontally. This means corresponding nodes at the same level should have equal values, and their children should be mirror images of each other.
Think of it like holding up a tree structure to a mirror - does the reflection match the original perfectly?
You're given the
root of a binary tree and need to determine if it's symmetric around its center - meaning the left subtree is a mirror reflection of the right subtree. A symmetric tree has the property that if you draw a vertical line through the root, the left side is exactly the same as the right side, but flipped horizontally. This means corresponding nodes at the same level should have equal values, and their children should be mirror images of each other.
Think of it like holding up a tree structure to a mirror - does the reflection match the original perfectly?
Input & Output
example_1.py — Perfect Symmetric Tree
$
Input:
[1,2,2,3,4,4,3]
›
Output:
true
💡 Note:
The tree is perfectly symmetric. The left subtree [2,3,4] mirrors the right subtree [2,4,3]. Each node on the left has a corresponding node with the same value at the mirrored position on the right.
example_2.py — Asymmetric Tree
$
Input:
[1,2,2,null,3,null,3]
›
Output:
false
💡 Note:
The tree is not symmetric. The left subtree has a right child with value 3, while the corresponding mirror position (left child of right subtree) is null. The structure doesn't match.
example_3.py — Single Node Tree
$
Input:
[1]
›
Output:
true
💡 Note:
A single node tree is always symmetric as there are no children to compare. The tree trivially satisfies the mirror property.
Constraints
- The number of nodes in the tree is in the range [1, 1000]
- -100 ≤ Node.val ≤ 100
- Tree structure can vary - not necessarily complete or balanced
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code