Check if Two Trees are Symmetric in DSA Python - Time & Space Complexity
We want to know how long it takes to check if two trees are mirror images of each other.
How does the time needed grow as the trees get bigger?
Analyze the time complexity of the following code snippet.
def is_symmetric(t1, t2):
if not t1 and not t2:
return True
if not t1 or not t2:
return False
if t1.val != t2.val:
return False
return is_symmetric(t1.left, t2.right) and is_symmetric(t1.right, t2.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 pair in both trees.
Each node in the trees is checked once against its mirror node.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 node comparisons |
| 100 | About 100 node comparisons |
| 1000 | About 1000 node comparisons |
Pattern observation: The number of operations grows roughly the same as the number of nodes.
Time Complexity: O(n)
This means the time to check symmetry grows linearly with the 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 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 the trees were not binary but had multiple children? 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 AQuick Check:
Symmetry means mirror image = A [OK]
- Thinking same number of nodes means symmetric
- Ignoring node values in comparison
- Checking only left or right subtree
Solution
Step 1: Check syntax for function definition
Python functions require parentheses around parameters and a colon at the end.Step 2: Identify correct type hint usage
def is_symmetric(tree1, tree2) -> bool: uses correct syntax with type hint and colon, making it valid and clear.Final Answer:
def is_symmetric(tree1, tree2) -> bool: -> Option CQuick Check:
Correct Python function syntax with type hint = D [OK]
- Missing comma between parameters
- Omitting colon at function end
- Incorrect parameter syntax without parentheses
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def is_mirror(t1, t2):
if not t1 and not t2:
return True
if not t1 or not t2:
return False
return (t1.val == t2.val) and is_mirror(t1.left, t2.right) and is_mirror(t1.right, t2.left)
root1 = Node(1)
root1.left = Node(2)
root1.right = Node(3)
root2 = Node(1)
root2.left = Node(3)
root2.right = Node(2)
print(is_mirror(root1, root2))Solution
Step 1: Check base cases for empty nodes
Both trees have root 1, and their left and right children are swapped but equal in value.Step 2: Recursively verify mirror condition
Left of root1 (2) matches right of root2 (2), and right of root1 (3) matches left of root2 (3), so all conditions return True.Final Answer:
True -> Option AQuick Check:
Mirror check returns True for swapped children = A [OK]
- Confusing left and right children in recursion
- Ignoring base cases for None nodes
- Assuming same structure means symmetric
def is_symmetric(t1, t2):
if t1 is None and t2 is None:
return True
if t1 is None or t2 is None:
return False
if t1.val != t2.val:
return False
return is_symmetric(t1.left, t2.left) and is_symmetric(t1.right, t2.right)Solution
Step 1: Analyze recursive calls for symmetry
The function compares left subtree of t1 with left subtree of t2 and right with right, which is incorrect for mirror symmetry.Step 2: Correct mirror comparison
It should compare left of t1 with right of t2 and right of t1 with left of t2 to check mirror symmetry properly.Final Answer:
It compares left with left and right with right instead of mirror sides. -> Option BQuick Check:
Mirror check requires cross comparison = B [OK]
- Comparing same sides instead of mirror sides
- Confusing 'is' and '==' for values
- Missing base cases for None nodes
Solution
Step 1: Handle None edge cases carefully
If both trees are None, they are symmetric by definition, so return True.Step 2: Recursively check mirror symmetry
If only one is None, return False. Otherwise, compare root values and recursively check left of one with right of other and vice versa.Final Answer:
Return True if both trees are None, else check mirror recursively. -> Option DQuick Check:
Both None means symmetric, else recurse mirror check = C [OK]
- Returning False if either tree is None without checking both
- Skipping recursive mirror checks
- Comparing only root values
Ready to prove it?
Pass the test and this pattern moves unknown
