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.

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
Symmetric Tree - Mirror Tree Checker INPUT Binary Tree Structure 1 2 2 3 4 4 3 Mirror Line Input Array: [1, 2, 2, 3, 4, 4, 3] ALGORITHM STEPS 1 Check Root If root is null, return true 2 Compare Subtrees Call isMirror(left, right) 3 Mirror Check node1.val == node2.val left.left == right.right left.right == right.left 4 Recursive Verify Recursively check all pairs Comparisons: 2==2, 3==3, 4==4 All pairs match! Time: O(n) | Space: O(h) FINAL RESULT Symmetric Tree Verified 1 2 2 mirror 3 4 4 3 Output: true OK - Symmetric! Key Insight: A tree is symmetric if its left and right subtrees are mirror images. Compare nodes in reverse order: left.left with right.right, and left.right with right.left. Both must match at every level for symmetry. isMirror(t1, t2) = (t1.val == t2.val) AND isMirror(t1.left, t2.right) AND isMirror(t1.right, t2.left) TutorialsPoint - Symmetric Tree | Optimal Recursive Solution
Asked in
Google 45 Amazon 38 Microsoft 32 Apple 28
82.4K 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