What if you could instantly tell if two complex trees are perfect mirror images without getting lost in details?
Why Check if Two Trees are Symmetric in DSA Java?
Imagine you have two family trees drawn on paper, and you want to see if one is a mirror image of the other. Doing this by looking at every branch and leaf manually is tiring and confusing.
Manually comparing each node and its position is slow and easy to mess up. You might miss a branch or compare nodes in the wrong order, leading to wrong conclusions.
Using a smart method to check symmetry lets the computer compare both trees step-by-step, matching nodes on opposite sides automatically and quickly.
boolean isSymmetric(TreeNode t1, TreeNode t2) {
// manually check each node and position
// complex and error-prone
return false;
}boolean isSymmetric(TreeNode root) {
return isMirror(root, root);
}
boolean isMirror(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null) return false;
return (t1.val == t2.val) && isMirror(t1.left, t2.right) && isMirror(t1.right, t2.left);
}This lets you quickly and reliably check if two trees are mirror images, which is useful in many tree-based problems.
Checking if two organizational charts are symmetric can help spot errors or confirm balanced team structures.
Manual comparison is slow and error-prone.
Recursive checking matches nodes on opposite sides automatically.
Enables fast and reliable symmetry checks in trees.
Practice
Solution
Step 1: Understand symmetry in trees
Two trees are symmetric if one is the mirror reflection of the other around the center.Step 2: Compare structure and node values
Both structure and node values must match in a mirrored way for symmetry.Final Answer:
They are mirror images of each other around their center. -> Option CQuick Check:
Symmetry means mirror image = D [OK]
- Thinking same number of nodes means symmetric
- Only comparing root values
- Ignoring structure differences
Solution
Step 1: Identify correct return type and parameters
The method should return boolean and accept two TreeNode parameters for two trees.Step 2: Check method signature syntax
Public access, boolean return, and parameter types match in public boolean isSymmetric(TreeNode root1, TreeNode root2).Final Answer:
public boolean isSymmetric(TreeNode root1, TreeNode root2) -> Option AQuick Check:
Correct signature returns boolean with two TreeNode inputs [OK]
- Using void return type
- Missing public keyword
- Using only one TreeNode parameter
TreeNode root1 = new TreeNode(1);
root1.left = new TreeNode(2);
root1.right = new TreeNode(3);
TreeNode root2 = new TreeNode(1);
root2.left = new TreeNode(3);
root2.right = new TreeNode(2);
System.out.println(isSymmetric(root1, root2));
public static boolean isSymmetric(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null) return false;
return (t1.val == t2.val) && isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}Solution
Step 1: Analyze tree structure and values
root1 has left=2, right=3; root2 has left=3, right=2, which are mirror images.Step 2: Trace recursive calls
Values match and recursive calls check mirrored children, all return true.Final Answer:
true -> Option DQuick Check:
Mirror structure and values = true [OK]
- Ignoring mirrored child comparison
- Confusing left and right children
- Assuming same structure means false
public boolean isSymmetric(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null) return false;
return (t1.val == t2.val) && isSymmetric(t1.left, t2.left) && isSymmetric(t1.right, t2.right);
}Solution
Step 1: Review recursive calls for symmetry
The code compares t1.left with t2.left and t1.right with t2.right, which checks identical structure, not mirror.Step 2: Identify correct mirrored comparison
It should compare t1.left with t2.right and t1.right with t2.left for symmetry.Final Answer:
It compares left with left and right with right instead of mirrored children. -> Option AQuick Check:
Mirror check needs cross child comparison [OK]
- Comparing same side children instead of mirrored
- Ignoring null checks
- Wrong return type usage
Solution
Step 1: Understand symmetry check requirements
Symmetry means one tree is mirror image of the other, so left subtree of one matches right subtree of other.Step 2: Evaluate approaches
Recursively check if left subtree of first tree is mirror of right subtree of second tree and vice versa. correctly uses recursive mirror checks; others ignore structure or order.Final Answer:
Recursively check if left subtree of first tree is mirror of right subtree of second tree and vice versa. -> Option BQuick Check:
Mirror recursive check solves symmetry [OK]
- Using preorder traversal comparison
- Comparing only height or leaf counts
- Ignoring mirrored subtree checks
Ready to prove it?
Pass the test and this pattern moves unknown
