Check if Two Trees are Symmetric in DSA Typescript - 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.
function isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1.val === t2.val) &&
isSymmetric(t1.left, t2.right) &&
isSymmetric(t1.right, t2.left);
}
This function 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, visiting all nodes.
As the number of nodes grows, the function checks each node pair once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 node comparisons |
| 100 | About 100 node comparisons |
| 1000 | About 1000 node comparisons |
Pattern observation: The work grows directly with the number of nodes.
Time Complexity: O(n)
This means the time to check symmetry grows in a straight line with the number of nodes.
[X] Wrong: "The function only checks half the nodes, 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 all nodes.
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 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 root node.Step 2: Differentiate from other properties
Having the same number of nodes or values in order does not guarantee symmetry; symmetry requires mirrored structure and values.Final Answer:
They are mirror images of each other around their root. -> Option AQuick Check:
Symmetry means mirror image = A [OK]
- Confusing symmetry with identical trees
- Thinking same node count means symmetry
- Assuming balanced trees are always symmetric
Solution
Step 1: Identify correct parameters and return type
The function should take two tree roots and return a boolean indicating symmetry.Step 2: Check function signature correctness
function isSymmetric(root1: TreeNode, root2: TreeNode): boolean matches this: two TreeNode parameters and boolean return type.Final Answer:
function isSymmetric(root1: TreeNode, root2: TreeNode): boolean -> Option BQuick Check:
Two roots + boolean return = D [OK]
- Using only one root parameter
- Returning number instead of boolean
- Using void return type
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function isMirror(t1: TreeNode | null, t2: TreeNode | null): boolean {
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);
}
const tree1 = new TreeNode(1, new TreeNode(2), new TreeNode(3));
const tree2 = new TreeNode(1, new TreeNode(3), new TreeNode(2));
console.log(isMirror(tree1, tree2));Solution
Step 1: Understand the isMirror function logic
It checks if two trees are mirror images by comparing root values and recursively checking left of one with right of other and vice versa.Step 2: Trace the given trees
tree1 has root 1, left 2, right 3; tree2 has root 1, left 3, right 2. They are mirrors, so isMirror returns true.Final Answer:
true -> Option CQuick Check:
Mirror check returns true for these trees [OK]
- Confusing left and right child comparison
- Assuming same structure means mirror
- Ignoring null checks
function isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
if (t1.val !== t2.val) return false;
return isSymmetric(t1.left, t2.left) && isSymmetric(t1.right, t2.right);
}Solution
Step 1: Analyze recursive calls
The function compares t1.left with t2.left and t1.right with t2.right, which checks identical structure, not mirror symmetry.Step 2: Identify correct mirror comparison
For symmetry, t1.left should be compared with t2.right and t1.right with t2.left.Final Answer:
It compares left with left and right with right instead of mirror sides. -> Option DQuick Check:
Mirror needs cross child comparison = A [OK]
- Comparing same side children instead of opposite
- Forgetting to check node values before recursion
- Missing null base cases
Solution
Step 1: Understand symmetry check requirements
Symmetry means the left subtree of one tree is the mirror of the right subtree of the other.Step 2: Choose correct approach
Recursively comparing left subtree of one with right subtree of the other ensures mirror symmetry is checked properly.Final Answer:
Use a helper function to recursively compare left subtree of one tree with right subtree of the other and vice versa. -> Option AQuick Check:
Mirror recursion approach = B [OK]
- Comparing preorder traversals which miss mirror structure
- Checking only height or node count
- Comparing only root values
Ready to prove it?
Pass the test and this pattern moves unknown
