Check if Two Trees are Symmetric in DSA Rust - 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.
fn is_symmetric(t1: &Option>, t2: &Option>) -> bool {
match (t1, t2) {
(None, None) => true,
(Some(n1), Some(n2)) => {
n1.val == n2.val &&
is_symmetric(&n1.left, &n2.right) &&
is_symmetric(&n1.right, &n2.left)
},
_ => false,
}
}
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 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows directly with 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, it still visits every node once, so the time grows with all nodes.
Understanding how recursive tree checks grow helps you explain your approach clearly and confidently in interviews.
"What if we changed the function to check only 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, meaning left subtree of one matches right subtree of the other and vice versa.Step 2: Compare structure and 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 = C [OK]
- Thinking same number of nodes means symmetric
- Only comparing root values
- Ignoring subtree structure
Solution
Step 1: Understand Rust tree node representation
Binary trees in Rust are often represented as Option to handle empty nodes safely.Step 2: Choose correct signature
Passing references to Option avoids ownership issues and returns bool for symmetric check.Final Answer:
fn is_symmetric(t1: &Option, t2: &Option) -> bool -> Option BQuick Check:
Use references to Option and bool return [OK]
- Passing TreeNode by value causing ownership errors
- Returning Option<bool> instead of bool
- Using non-Option types for nullable nodes
struct TreeNode {
val: i32,
left: Option,
right: Option,
}
fn is_mirror(t1: &Option, t2: &Option) -> bool {
match (t1, t2) {
(None, None) => true,
(Some(n1), Some(n2)) => {
n1.val == n2.val &&
is_mirror(&n1.left, &n2.right) &&
is_mirror(&n1.right, &n2.left)
},
_ => false,
}
}
fn main() {
let tree1 = Some(Box::new(TreeNode {
val: 1,
left: Some(Box::new(TreeNode { val: 2, left: None, right: None })),
right: Some(Box::new(TreeNode { val: 3, left: None, right: None })),
}));
let tree2 = Some(Box::new(TreeNode {
val: 1,
left: Some(Box::new(TreeNode { val: 3, left: None, right: None })),
right: Some(Box::new(TreeNode { val: 2, left: None, right: None })),
}));
println!("{}", is_mirror(&tree1, &tree2));
}Solution
Step 1: Analyze tree structure
tree1 has root 1, left child 2, right child 3; tree2 has root 1, left child 3, right child 2.Step 2: Check mirror condition
Left subtree of tree1 (2) matches right subtree of tree2 (2), and right subtree of tree1 (3) matches left subtree of tree2 (3), values match.Final Answer:
true -> Option DQuick Check:
Mirror check returns true [OK]
- Confusing left and right subtree positions
- Assuming same structure means symmetric
- Ignoring node values in comparison
fn is_mirror(t1: &Option, t2: &Option) -> bool {
if t1.is_none() && t2.is_none() {
return true;
}
if t1.is_none() || t2.is_none() {
return false;
}
let n1 = t1.as_ref().unwrap();
let n2 = t2.as_ref().unwrap();
n1.val == n2.val &&
is_mirror(&n1.left, &n2.left) &&
is_mirror(&n1.right, &n2.right)
}Solution
Step 1: Review subtree comparisons
The function compares n1.left with n2.left and n1.right with n2.right, which checks for equality, not mirror symmetry.Step 2: Identify correct mirror check
For symmetry, n1.left should be compared with n2.right and n1.right with n2.left.Final Answer:
It compares left subtree with left subtree instead of left with right. -> Option AQuick Check:
Mirror check swaps left and right subtrees [OK]
- Comparing left with left and right with right
- Forgetting to check both subtrees recursively
- Ignoring None cases
is_mirror to solve this?Solution
Step 1: Understand single tree symmetry
A tree is symmetric if its left subtree is a mirror of its right subtree.Step 2: Use is_mirror function correctly
Comparing root.left and root.right subtrees with is_mirror checks this mirror condition.Final Answer:
Call is_mirror(&root.left, &root.right) to compare left and right subtrees. -> Option CQuick Check:
Compare left and right subtrees for symmetry [OK]
- Comparing tree with itself instead of subtrees
- Comparing left subtree with itself
- Comparing right subtree with itself
Ready to prove it?
Pass the test and this pattern moves unknown
