1class Solution {
2 public boolean isSymmetric(TreeNode root) {
3 // An empty tree is symmetric
4 if (root == null) return true;
5 // Use a helper function to compare left and right subtree
6 return isMirror(root.left, root.right);
7 }
8
9 private boolean isMirror(TreeNode t1, TreeNode t2) {
10 // If both nodes are null, they are mirrors
11 if (t1 == null && t2 == null) return true;
12 // If one node is null, not mirrors
13 if (t1 == null || t2 == null) return false;
14 // Check values and recurse on children, swapping roles
15 return (t1.val == t2.val) &&
16 isMirror(t1.right, t2.left) &&
17 isMirror(t1.left, t2.right);
18 }
19}