What does it mean for two binary trees to be symmetric?
Two binary trees are symmetric if one is the mirror image of the other. This means the left subtree of one tree is a mirror reflection of the right subtree of the other tree, and vice versa.
Click to reveal answer
beginner
What is the base case when checking if two trees are symmetric?
If both nodes being compared are null, they are symmetric at that point. If only one is null, they are not symmetric.
Click to reveal answer
intermediate
How do you recursively check if two trees are symmetric?
Check if the current nodes have the same value, then recursively check if the left child of the first tree is symmetric with the right child of the second tree, and the right child of the first tree is symmetric with the left child of the second tree.
Click to reveal answer
intermediate
Why is recursion a good approach to check tree symmetry?
Because symmetry involves comparing subtrees in a mirrored way, recursion naturally allows checking corresponding nodes and their children step-by-step.
Click to reveal answer
intermediate
What is the time complexity of checking if two trees are symmetric?
The time complexity is O(n), where n is the number of nodes in the trees, because each node is visited once during the comparison.
Click to reveal answer
What should you return if both nodes being compared are null when checking symmetry?
ATrue
BFalse
CCheck children
DReturn null
✗ Incorrect
If both nodes are null, they are symmetric at that point, so return true.
Which pairs of nodes do you compare to check symmetry?
ALeft child of first tree with left child of second tree
BLeft child of first tree with right child of second tree
CRoot nodes only
DRight child of first tree with right child of second tree
✗ Incorrect
Symmetry means the left subtree of one tree matches the right subtree of the other, so compare left child of first tree with right child of second tree.
What happens if one node is null and the other is not during symmetry check?
ATrees are not symmetric
BTrees are symmetric
CContinue checking
DIgnore null nodes
✗ Incorrect
If one node is null and the other is not, the trees differ in structure and are not symmetric.
What is the first step in the recursive symmetry check?
ACompare node values
BTraverse left subtree
CCheck if nodes are null
DTraverse right subtree
✗ Incorrect
First check if nodes are null to handle base cases before comparing values.
What is the overall time complexity of checking if two trees are symmetric?
AO(n^2)
BO(log n)
CO(1)
DO(n)
✗ Incorrect
Each node is visited once, so the time complexity is linear, O(n).
Explain how to check if two binary trees are symmetric using recursion.
Think about mirror image and how left and right children relate.
You got /4 concepts.
Describe the importance of base cases when checking tree symmetry.
Base cases stop recursion and handle edge conditions.
You got /4 concepts.
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.