Check if Two Trees are Symmetric in DSA Java - 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 boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return isMirror(root.left, root.right);
}
private boolean 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);
}
This code checks if a tree is symmetric by comparing its left and right subtrees 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 work 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, 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 the tree is not balanced? 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 = D [OK]
- Thinking same number of nodes means symmetric
- Only comparing root values
- Ignoring structure differences
Solution
Step 1: Identify correct return type and parameters
The method should return boolean and accept two TreeNode parameters for two trees.Step 2: Check method signature syntax
Public access, boolean return, and parameter types match in public boolean isSymmetric(TreeNode root1, TreeNode root2).Final Answer:
public boolean isSymmetric(TreeNode root1, TreeNode root2) -> Option AQuick Check:
Correct signature returns boolean with two TreeNode inputs [OK]
- Using void return type
- Missing public keyword
- Using only one TreeNode parameter
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);
System.out.println(isSymmetric(root1, root2));
public static boolean isSymmetric(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null) return false;
return (t1.val == t2.val) && isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}Solution
Step 1: Analyze tree structure and values
root1 has left=2, right=3; root2 has left=3, right=2, which are mirror images.Step 2: Trace recursive calls
Values match and recursive calls check mirrored children, all return true.Final Answer:
true -> Option DQuick Check:
Mirror structure and values = true [OK]
- Ignoring mirrored child comparison
- Confusing left and right children
- Assuming same structure means false
public boolean isSymmetric(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null) return false;
return (t1.val == t2.val) && isSymmetric(t1.left, t2.left) && isSymmetric(t1.right, t2.right);
}Solution
Step 1: Review recursive calls for symmetry
The code compares t1.left with t2.left and t1.right with t2.right, which checks identical structure, not mirror.Step 2: Identify correct mirrored comparison
It should compare t1.left with t2.right and t1.right with t2.left for symmetry.Final Answer:
It compares left with left and right with right instead of mirrored children. -> Option AQuick Check:
Mirror check needs cross child comparison [OK]
- Comparing same side children instead of mirrored
- Ignoring null checks
- Wrong return type usage
Solution
Step 1: Understand symmetry check requirements
Symmetry means one tree is mirror image of the other, so left subtree of one matches right subtree of other.Step 2: Evaluate approaches
Recursively check if left subtree of first tree is mirror of right subtree of second tree and vice versa. correctly uses recursive mirror checks; others ignore structure or order.Final Answer:
Recursively check if left subtree of first tree is mirror of right subtree of second tree and vice versa. -> Option BQuick Check:
Mirror recursive check solves symmetry [OK]
- Using preorder traversal comparison
- Comparing only height or leaf counts
- Ignoring mirrored subtree checks
Ready to prove it?
Pass the test and this pattern moves unknown
