Check if Two Trees are Symmetric in DSA C++ - Time & Space Complexity
We want to understand how the time needed to check if two trees are mirror images grows as the trees get bigger.
How does the number of steps change when the trees have more nodes?
Analyze the time complexity of the following code snippet.
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val)
&& isMirror(t1->left, t2->right)
&& isMirror(t1->right, t2->left);
}
bool isSymmetric(TreeNode* root) {
return isMirror(root, root);
}
This code checks if a tree is symmetric by comparing its left and right sides recursively.
- Primary operation: Recursive calls comparing pairs of nodes.
- How many times: Once for each node in the tree, visiting all nodes.
Each node is checked once against its mirror node, so the steps grow as the number of nodes grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of steps 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 nodes, so it runs in half the time."
[OK] Correct: Even though it compares pairs, every node is visited once, so the time still grows with all nodes.
Understanding how recursive tree checks scale 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 = A [OK]
- Thinking same number of nodes means symmetric
- Only comparing root values
- Ignoring structure when checking symmetry
Solution
Step 1: Identify correct return type
The function should return a boolean indicating symmetry, so return type is bool.Step 2: Use pointers for tree nodes
Tree nodes are usually passed as pointers to avoid copying and to handle null nodes.Final Answer:
bool isSymmetric(TreeNode* root1, TreeNode* root2); -> Option BQuick Check:
Return bool and use pointers = C [OK]
- Using void return type
- Passing nodes by value instead of pointer
- Using int instead of bool for result
#include <iostream>
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
int main() {
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);
if (isMirror(root1, root2))
std::cout << "Symmetric";
else
std::cout << "Not Symmetric";
return 0;
}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
Values match and recursive calls check left-right and right-left subtrees, confirming symmetry.Final Answer:
Symmetric -> Option AQuick Check:
Mirror check returns true = Symmetric [OK]
- Confusing left and right subtree checks
- Ignoring null node base cases
- Assuming same structure means symmetric
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirror(t1->left, t2->left) && isMirror(t1->right, t2->right);
}Solution
Step 1: Review recursive calls for symmetry
Symmetry requires comparing left subtree of one tree with right subtree of the other.Step 2: Identify incorrect subtree comparisons
The code compares left with left and right with right, which checks for identical trees, not symmetric.Final Answer:
It compares left subtree with left subtree instead of left with right. -> Option DQuick Check:
Mirror needs left-right swap, code misses it = D [OK]
- Comparing same side subtrees instead of mirrored sides
- Ignoring null checks
- Confusing identical and symmetric trees
Solution
Step 1: Understand symmetry check logic
Symmetry means one tree is mirror of the other, so compare left subtree of one with right subtree of other recursively.Step 2: Evaluate other approaches
Preorder traversal or node count alone do not guarantee symmetry; level order without mirroring misses mirror structure.Final Answer:
Use a recursive function that compares left subtree of first tree with right subtree of second tree and vice versa. -> Option AQuick Check:
Recursive mirror comparison solves symmetry = A [OK]
- Using simple traversal without mirroring
- Comparing only node counts or root values
- Ignoring subtree structure in mirror form
Ready to prove it?
Pass the test and this pattern moves unknown
