Tree Twins: Are You a Mirror?

Mirror Check – A Symmetric Tree Validation Problem

Problem Statement

Given the root of a binary tree, determine whether the tree is symmetric around its center. A tree is symmetric if the left subtree is a mirror reflection of the right subtree.

Example

Input:
root = [1,2,2,3,4,4,3]
Output:
true

Explanation:
The left and right subtrees of the root are mirror images at every level. Hence, the tree is symmetric.

Best Data Structure for the Job

This problem is fundamentally about comparing two subtrees. A recursive traversal is natural for tree structures, allowing easy mirroring checks between corresponding left and right children. For an iterative solution, a queue can be used to compare nodes level by level in a mirrored fashion.

Different Approaches

Approach 1: Recursive Mirror Comparison

Use a helper function that recursively checks if the left and right subtrees are mirrors of each other.

class TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } } public class SymmetricTree { public boolean isSymmetric(TreeNode root) { if (root == null) return true; return isMirror(root.left, root.right); } private boolean isMirror(TreeNode t1, TreeNode t2) { if (t1 == null && t2 == null) return true; if (t1 == null || t2 == null || t1.val != t2.val) return false; return isMirror(t1.left, t2.right) && isMirror(t1.right, t2.left); } }

Approach 2: Iterative with Queue

Use a queue to iteratively compare nodes in pairs, ensuring they are mirror images.

import java.util.LinkedList; import java.util.Queue; public class SymmetricTreeIterative { public boolean isSymmetric(TreeNode root) { if (root == null) return true; Queue<TreeNode> queue = new LinkedList<>(); queue.add(root.left); queue.add(root.right); while (!queue.isEmpty()) { TreeNode t1 = queue.poll(); TreeNode t2 = queue.poll(); if (t1 == null && t2 == null) continue; if (t1 == null || t2 == null || t1.val != t2.val) return false; queue.add(t1.left); queue.add(t2.right); queue.add(t1.right); queue.add(t2.left); } return true; } }

⚠️Common Mistakes and Pitfalls

  • Comparing left.left with right.left instead of left.left with right.right, which breaks the mirror logic.

  • Forgetting to handle null node cases early.

  • Assuming the tree is symmetric if node values match, without validating structure.

📊Time and Space Complexity Analysis

Approach        Time ComplexitySpace Complexity
Recursive        O(n)        O(h) where h is tree height
Iterative (Queue)        O(n)        O(n)

🌍Real-world Applications

  • Validation of visual layouts or symmetrical user interface designs.

  • Mirror-based rendering or layout engines.

  • Tree comparison in compilers or structured data validation tools.


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Trailing Zeros in Factorial – How Many and Why?

Divisible Drama — Who’s In, Who’s Out?

Zero Array After Queries