What if you could instantly tell if two complex trees are perfect mirror images without lifting a finger?
Why Check if Two Trees are Symmetric in DSA C#?
Imagine you have two family trees drawn on paper, and you want to see if one is a mirror image of the other. You try to compare each branch and leaf by looking back and forth manually.
Manually checking each branch and leaf is slow and confusing. You might miss small differences or get tired, leading to mistakes. It's hard to keep track of which parts you already compared.
Using a program to check if two trees are symmetric saves time and avoids errors. The program can compare branches step-by-step automatically, making sure every part matches perfectly as a mirror image.
bool AreSymmetric(TreeNode root1, TreeNode root2) {
// Manually compare nodes one by one
// Complex and error-prone
return false; // placeholder
}bool AreSymmetric(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) return true;
if (root1 == null || root2 == null) return false;
return root1.val == root2.val &&
AreSymmetric(root1.left, root2.right) &&
AreSymmetric(root1.right, root2.left);
}This lets you quickly and reliably check if two trees are mirror images, enabling better data validation and tree-based problem solving.
In graphics or UI design, checking if two layouts or structures are symmetric helps create balanced and visually pleasing designs automatically.
Manual comparison of trees is slow and error-prone.
Recursive checking automates and simplifies symmetry verification.
Symmetry checks help in many fields like design, data validation, and algorithms.
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 [OK]
- Thinking symmetry means identical trees
- Only comparing root values
- Ignoring structure in comparison
Solution
Step 1: Determine return type for symmetry check
The function should return true or false, so return type must be bool.Step 2: Check method signature correctness
Method with signature public bool IsSymmetric(TreeNode root1, TreeNode root2) matches expected usage.Final Answer:
public bool IsSymmetric(TreeNode root1, TreeNode root2) -> Option DQuick Check:
Return type bool for true/false [OK]
- Using void return type
- Returning int or string instead of bool
- Incorrect parameter types
class TreeNode {
public int val;
public TreeNode left, right;
public TreeNode(int x) { val = x; }
}
bool 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);
}
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);
Console.WriteLine(IsMirror(root1, root2));Solution
Step 1: Analyze tree structures
root1 has left=2, right=3; root2 has left=3, right=2, which are mirror images.Step 2: Trace IsMirror function calls
IsMirror compares root values (1==1), then left of root1 with right of root2 (2==2), and right of root1 with left of root2 (3==3), all true.Final Answer:
True -> Option AQuick Check:
Mirror check returns true [OK]
- Confusing left and right children
- Assuming identical structure means symmetric
- Ignoring null checks
bool IsMirror(TreeNode t1, TreeNode t2) {
if (t1 == null || t2 == null) return true;
if (t1.val != t2.val) return false;
return IsMirror(t1.left, t2.right) && IsMirror(t1.right, t2.left);
}Solution
Step 1: Check null node handling
If one node is null and the other is not, trees are not symmetric, so should return false, not true.Step 2: Verify other conditions
Value comparison and recursive calls are correct for mirror checking.Final Answer:
The first if condition should return false if one node is null and the other is not. -> Option AQuick Check:
Null mismatch means false [OK]
- Returning true when one node is null
- Mixing up recursive child comparisons
- Incorrect return types
Solution
Step 1: Understand symmetry check logic
Symmetry means left subtree of one tree matches right subtree of the other and vice versa.Step 2: Evaluate approaches
Recursive helper comparing opposite subtrees is efficient and correct. Preorder or level order traversals do not capture mirror structure properly.Final Answer:
Use a recursive helper that compares left subtree of first tree with right subtree of second tree and vice versa. -> Option BQuick Check:
Mirror recursion solves symmetry [OK]
- Comparing same side subtrees instead of opposite
- Using traversal equality which misses mirror structure
- Ignoring node values in comparison
Ready to prove it?
Pass the test and this pattern moves unknown
