Check if Two Trees are Symmetric in DSA C# - Time & Space Complexity
We want to know how the time needed to check if two trees are mirror images grows as the trees get bigger.
How does the work increase when the trees have more nodes?
Analyze the time complexity of the following code snippet.
public bool AreSymmetric(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) return true;
if (root1 == null || root2 == null) return false;
if (root1.val != root2.val) return false;
return AreSymmetric(root1.left, root2.right) && AreSymmetric(root1.right, root2.left);
}
This code checks if two trees are mirror images by comparing nodes recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls comparing pairs of nodes.
- How many times: Once for each node in the trees combined.
Each node pair is checked once, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows in a straight line as the number of nodes increases.
Time Complexity: O(n)
This means the time to check symmetry grows directly with the total number of nodes in the trees.
[X] Wrong: "The function only checks half the nodes, so it runs in half the time."
[OK] Correct: Even though it compares nodes in pairs, every node is visited once, so the time still grows with all nodes.
Understanding how recursion explores tree nodes helps you explain your approach clearly and confidently in interviews.
"What if we changed the code to check symmetry iteratively using a queue? How would the time complexity change?"
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
