Check if Two Trees are Symmetric in DSA Go - 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 number of steps change when the trees have more nodes?
Analyze the time complexity of the following code snippet.
func isSymmetric(t1, t2 *TreeNode) bool {
if t1 == nil && t2 == nil {
return true
}
if t1 == nil || t2 == nil {
return false
}
return t1.Val == t2.Val && isSymmetric(t1.Left, t2.Right) && isSymmetric(t1.Right, t2.Left)
}
This code checks if two trees are mirror images by comparing nodes and their opposite children 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 tree.
Each node pair is checked once, so the steps grow directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows in a straight line as the tree size grows.
Time Complexity: O(n)
This means the time to check symmetry grows directly 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 nodes in 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 only one subtree against itself instead of two? 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 [OK]
- Thinking same number of nodes means symmetric
- Only comparing root values
- Ignoring structure differences
Solution
Step 1: Identify correct parameter types
Function should take pointers to TreeNode structs for both trees to compare.Step 2: Identify correct return type
Return type should be bool to indicate symmetric (true) or not (false).Final Answer:
func isSymmetric(t1 *TreeNode, t2 *TreeNode) bool -> Option CQuick Check:
Pointer params and bool return [OK]
- Using value instead of pointer for trees
- Returning int or string instead of bool
- Missing one tree parameter
package main
import "fmt"
type TreeNode struct {
Val int
Left, Right *TreeNode
}
func isSymmetric(t1, t2 *TreeNode) bool {
if t1 == nil && t2 == nil {
return true
}
if t1 == nil || t2 == nil {
return false
}
return t1.Val == t2.Val && isSymmetric(t1.Left, t2.Right) && isSymmetric(t1.Right, t2.Left)
}
func main() {
root1 := &TreeNode{Val: 1, Left: &TreeNode{Val: 2}, Right: &TreeNode{Val: 2}}
root2 := &TreeNode{Val: 1, Left: &TreeNode{Val: 2}, Right: &TreeNode{Val: 3}}
fmt.Println(isSymmetric(root1, root2))
}Solution
Step 1: Compare root nodes
Both roots have value 1, so check children.Step 2: Compare left and right subtrees
Left subtree of root1 has value 2, right subtree of root2 has value 3, which differ.Final Answer:
false -> Option BQuick Check:
Different child values mean not symmetric [OK]
- Assuming same root means symmetric
- Ignoring mismatch in child nodes
- Confusing symmetric with identical
func isSymmetric(t1, t2 *TreeNode) bool {
if t1 == nil && t2 == nil {
return true
}
if t1 == nil || t2 == nil {
return false
}
return t1.Val == t2.Val && isSymmetric(t1.Left, t2.Left) && isSymmetric(t1.Right, t2.Right)
}Solution
Step 1: Check recursive calls for symmetry
Symmetry requires comparing left of one tree with right of the other, but code compares left with left and right with right.Step 2: Identify correct recursive calls
Correct calls should be isSymmetric(t1.Left, t2.Right) and isSymmetric(t1.Right, t2.Left).Final Answer:
The recursive calls compare wrong child nodes for symmetry. -> Option DQuick Check:
Mirror children must be compared [OK]
- Comparing left with left and right with right
- Missing nil base cases
- Returning wrong type
Solution
Step 1: Handle nil tree edge cases
If one tree is nil and the other is not, they cannot be symmetric, so return false immediately.Step 2: Ensure correct base cases in recursion
Only return true if both trees are nil at the same time, otherwise false.Final Answer:
Return false immediately if one tree is nil and the other is not. -> Option AQuick Check:
Nil mismatch means not symmetric [OK]
- Returning true when one tree is nil
- Skipping nil checks causing runtime errors
- Comparing only node counts
Ready to prove it?
Pass the test and this pattern moves unknown
