Mirror Tree Checker: Imagine looking at a binary tree and asking, "Is this tree a perfect reflection of itself?"

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
Tree Symmetry VisualizationAxis of Symmetry1223443Mirror MatchMirror MatchMirror Pairs:β€’ Left(2) ↔ Right(2) βœ“β€’ Left.left(3) ↔ Right.right(3) βœ“β€’ Left.right(4) ↔ Right.left(4) βœ“All pairs match β†’ Symmetric!Algorithm Steps:1. Compare root's left & right children2. Recursively check mirror positions3. Return true if all pairs matchTime: O(n) | Space: O(h)
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

n
2n
βœ“ Linear Growth
Space Complexity
O(n)

Stores two serialized strings, each potentially containing all node values

n
2n
⚑ 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
Asked in
Google 45 Amazon 38 Microsoft 32 Apple 28
82.3K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen