Discover how a simple trick can save you hours of confusing tree comparisons!
Why Check if Two Trees are Symmetric in DSA Go?
Imagine you have two family trees drawn on paper, and you want to see if they are mirror images of each other. Doing this by looking at every branch and leaf manually is tiring and confusing.
Manually comparing each node and its children in two trees is slow and easy to mess up. You might miss a branch or compare nodes in the wrong order, leading to wrong answers.
Using a simple algorithm, you can automatically check if two trees are symmetric by comparing nodes step-by-step in a mirrored way. This saves time and avoids mistakes.
func areSymmetricManual(t1, t2 *TreeNode) bool {
// Manually check each node and children one by one
if t1 == nil && t2 == nil {
return true
}
if t1 == nil || t2 == nil || t1.Val != t2.Val {
return false
}
return areSymmetricManual(t1.Left, t2.Right) && areSymmetricManual(t1.Right, t2.Left)
}func isSymmetric(root *TreeNode) bool {
var check func(t1, t2 *TreeNode) bool
check = func(t1, t2 *TreeNode) bool {
if t1 == nil && t2 == nil { return true }
if t1 == nil || t2 == nil || t1.Val != t2.Val { return false }
return check(t1.Left, t2.Right) && check(t1.Right, t2.Left)
}
return root == nil || check(root.Left, root.Right)
}This lets you quickly and correctly find out if two trees are mirror images, which is useful in many computer science problems.
Checking if two organizational charts are symmetric can help spot balanced team structures or errors in data entry.
Manual comparison of trees is slow and error-prone.
Algorithmic symmetry check compares nodes in mirrored order automatically.
This method saves time and ensures accuracy in tree comparisons.
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
