📖 Scenario: Imagine you have two family trees represented as binary trees. You want to check if these two trees are mirror images of each other, meaning they are symmetric in structure and values.
🎯 Goal: Build a program that creates two binary trees, sets up a helper method to check symmetry, and then uses this method to print whether the two trees are symmetric.
📋 What You'll Learn
Create two binary trees with exact node values and structure
Create a helper method called AreSymmetric that takes two tree nodes and returns true if they are symmetric
Use recursion to compare nodes in the two trees
Print "Symmetric" if the trees are symmetric, otherwise print "Not Symmetric"
💡 Why This Matters
🌍 Real World
Checking if two trees are symmetric is useful in graphics, data validation, and comparing hierarchical data like family trees or file systems.
💼 Career
Understanding tree structures and recursion is important for software development roles involving data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create Two Binary Trees
Create a class TreeNode with an int val, and two TreeNode references called left and right. Then create two trees called tree1 and tree2 with these exact structures and values:
tree1: root 1, left child 2, right child 3 tree2: root 1, left child 3, right child 2
DSA C#
Hint
Define the TreeNode class first. Then create tree1 and tree2 exactly as described.
2
Create Helper Method to Check Symmetry
Create a method called AreSymmetric that takes two TreeNode parameters called node1 and node2. This method should return true if both nodes are null, return false if only one is null or their values differ, and otherwise recursively check if node1.left is symmetric with node2.right and node1.right is symmetric with node2.left.
DSA C#
Hint
Use recursion to compare the left child of one node with the right child of the other, and vice versa.
3
Use the Helper Method to Check Trees
Create a boolean variable called result and set it by calling AreSymmetric(tree1, tree2).
DSA C#
Hint
Call the AreSymmetric method with tree1 and tree2 and store the answer in result.
4
Print the Result
Print "Symmetric" if result is true, otherwise print "Not Symmetric".
DSA C#
Hint
Use an if statement to print "Symmetric" when result is true, otherwise print "Not Symmetric".
Practice
(1/5)
1. What does it mean for two binary trees to be symmetric?
easy
A. They have the same number of nodes.
B. They have the same root value only.
C. They are mirror images of each other around their center.
D. They have identical left subtrees only.
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 C
Quick Check:
Symmetry means mirror image [OK]
Hint: Symmetric trees mirror each other exactly [OK]
Common Mistakes:
Thinking symmetry means identical trees
Only comparing root values
Ignoring structure in comparison
2. Which of the following C# method signatures correctly represents a function to check if two trees are symmetric?
easy
A. public void IsSymmetric(TreeNode root1, TreeNode root2)
B. public int IsSymmetric(TreeNode root1, TreeNode root2)
C. public string IsSymmetric(TreeNode root1, TreeNode root2)
D. public bool IsSymmetric(TreeNode root1, TreeNode root2)
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 D
Quick Check:
Return type bool for true/false [OK]
Hint: Symmetry check returns true or false, so use bool [OK]
Common Mistakes:
Using void return type
Returning int or string instead of bool
Incorrect parameter types
3. What will be the output of the following C# code snippet?
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));
medium
A. True
B. False
C. Compilation error
D. Runtime exception
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 A
Quick Check:
Mirror check returns true [OK]
Hint: Mirror check compares opposite children recursively [OK]
Common Mistakes:
Confusing left and right children
Assuming identical structure means symmetric
Ignoring null checks
4. Identify the bug in the following C# method that checks if two trees are symmetric:
A. The first if condition should return false if one node is null and the other is not.
B. The method should return void instead of bool.
C. The comparison t1.val != t2.val is incorrect and should be reversed.
D. The recursive calls should compare t1.left with t2.left and t1.right with t2.right.
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 A
Quick Check:
Null mismatch means false [OK]
Hint: Return false if only one node is null [OK]
Common Mistakes:
Returning true when one node is null
Mixing up recursive child comparisons
Incorrect return types
5. Given two binary trees, write a method to check if they are symmetric around their center. Which approach below correctly solves this problem efficiently?
hard
A. Use a queue to perform level order traversal and compare nodes at each level from left to right.
B. Use a recursive helper that compares left subtree of first tree with right subtree of second tree and vice versa.
C. Check if both trees have the same number of nodes and same root value only.
D. Traverse both trees in preorder and compare the traversal lists for equality.
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 B
Quick Check:
Mirror recursion solves symmetry [OK]
Hint: Compare opposite subtrees recursively for symmetry [OK]
Common Mistakes:
Comparing same side subtrees instead of opposite
Using traversal equality which misses mirror structure
Ignoring node values in comparison
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.