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.
Visualization
Tap to expand
Understanding the Visualization
1
Identify the Center Line
Draw a vertical line through the root node - this is our axis of symmetry
2
Compare Mirror Positions
For each node on the left, find its mirror position on the right and verify equal values
3
Check Structure
Ensure that if a node exists at position (level, offset), a corresponding node exists at (level, -offset)
Key Takeaway
π― Key Insight: Two synchronized traversals comparing mirror positions can verify tree symmetry efficiently in a single pass, avoiding the need for serialization or multiple tree scans.
Time & Space Complexity
Time Complexity
O(n)
Must traverse all n nodes to create serializations, plus string comparison
β Linear Growth
Space Complexity
O(n)
Stores two serialized strings, each potentially containing all node values
β‘ Linearithmic Space
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
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code