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.
bool isMirror(struct TreeNode* t1, struct 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);
}
bool isSymmetric(struct TreeNode* root) {
return isMirror(root, root);
}
This code checks if a tree is symmetric by comparing its left and right sides recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls comparing pairs of nodes from left and right subtrees.
- How many times: Once for each node in the tree, visiting all nodes to check symmetry.
As the tree grows, the function checks each node once by comparing pairs of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 comparisons |
| 100 | About 100 comparisons |
| 1000 | About 1000 comparisons |
Pattern observation: The number of operations grows roughly in direct proportion to the number of nodes.
Time Complexity: O(n)
This means the time to check symmetry grows linearly with the number of nodes in the tree.
[X] Wrong: "The function only checks half the tree, so it runs in half the time."
[OK] Correct: Even though it compares pairs, it still visits every node once, so the time grows with the full tree size.
Understanding how recursive tree checks scale helps you explain your approach clearly and confidently in interviews.
"What if we changed the function to check symmetry only on one subtree instead of both? 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 their roots.Step 2: Compare with other options
Having the same number of nodes, preorder traversal, or height does not guarantee mirror symmetry.Final Answer:
They are mirror images of each other around their roots -> Option AQuick Check:
Symmetry means mirror image = A [OK]
- Confusing same size with symmetry
- Thinking same traversal means symmetric
- Assuming same height means symmetric
Solution
Step 1: Understand pointer usage in C for trees
Tree nodes are accessed via pointers, so parameters must be pointers to struct Node.Step 2: Analyze each option
int isSymmetric(struct Node* root1, struct Node* root2); uses pointers correctly and returns int for boolean result. Options A and B pass structs by value, which is inefficient and incorrect. int isSymmetric(Node* root1, Node root2); mixes pointer and non-pointer types incorrectly.Final Answer:
int isSymmetric(struct Node* root1, struct Node* root2); -> Option AQuick Check:
Use pointers for tree nodes in C function [OK]
- Passing structs by value instead of pointers
- Mixing pointer and non-pointer parameters
- Using void return type instead of int for boolean
struct Node {
int val;
struct Node* left;
struct Node* right;
};
int isMirror(struct Node* t1, struct Node* t2) {
if (!t1 && !t2) return 1;
if (!t1 || !t2) return 0;
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
int main() {
struct Node n1 = {1, NULL, NULL};
struct Node n2 = {1, NULL, NULL};
printf("%d", isMirror(&n1, &n2));
return 0;
}Solution
Step 1: Analyze base cases and node values
Both nodes have value 1 and no children, so both left and right pointers are NULL.Step 2: Evaluate recursive calls
isMirror returns 1 if both nodes are NULL (true). Since both left and right children are NULL, recursive calls return 1. Values match, so overall returns 1.Final Answer:
1 -> Option BQuick Check:
Identical single nodes are symmetric = 1 [OK]
- Confusing NULL children as asymmetric
- Expecting 0 for identical nodes
- Misreading recursive base cases
int isMirror(struct Node* t1, struct Node* t2) {
if (t1 == NULL && t2 == NULL) return 1;
if (t1 == NULL || t2 == NULL) return 0;
if (t1->val != t2->val) return 0;
return isMirror(t1->left, t2->left) && isMirror(t1->right, t2->right);
}Solution
Step 1: Understand mirror symmetry check
Mirror symmetry requires left subtree of one tree to match right subtree of the other and vice versa.Step 2: Identify incorrect recursive calls
The code compares left with left and right with right, which checks for identical trees, not mirror symmetry.Final Answer:
Should compare t1->left with t2->right and t1->right with t2->left -> Option DQuick Check:
Mirror means cross compare left and right subtrees [OK]
- Comparing left with left and right with right
- Ignoring NULL checks
- Using wrong logical operators
Solution
Step 1: Understand iterative symmetry check
Use a queue to store pairs of nodes to compare their values and subtrees iteratively.Step 2: Identify correct node pairing
Pairs must be left child of one tree with right child of the other to check mirror symmetry.Step 3: Reject incorrect approaches
Preorder traversal comparison or using stack for one tree only does not check mirror symmetry. Ignoring children misses the point.Final Answer:
Enqueue pairs of nodes to compare left of one with right of other -> Option CQuick Check:
Queue pairs left-right nodes for iterative mirror check [OK]
- Comparing same side children instead of opposite
- Using preorder traversal arrays
- Ignoring children nodes in comparison
Ready to prove it?
Pass the test and this pattern moves unknown
