📖 Scenario: You are working with two simple binary trees representing family trees. You want to check if these two trees are mirror images of each other, meaning they are symmetric.
🎯 Goal: Build a Go program that creates two binary trees, sets a helper variable, writes a function to check if the trees are symmetric, and prints the result.
📋 What You'll Learn
Create two binary trees using the TreeNode struct with exact values
Create a helper variable result of type bool
Write a function isSymmetric that checks if two trees are symmetric
Print the value of result
💡 Why This Matters
🌍 Real World
Checking if two trees are symmetric is useful in comparing hierarchical data structures like family trees, organizational charts, or XML/JSON data.
💼 Career
Understanding tree symmetry helps in software development roles involving data structure manipulation, algorithm design, and problem-solving.
Progress0 / 4 steps
1
Create two binary trees
Create two binary trees using the TreeNode struct with these exact values: root1 with value 1, left child 2, right child 3; and root2 with value 1, left child 3, right child 2.
DSA Go
Hint
Use the &TreeNode{Val: value} syntax to create nodes and assign left and right children.
2
Create a helper variable
Create a boolean variable called result and set it to false inside the main function.
DSA Go
Hint
Use result := false to create and initialize the boolean variable.
3
Write the function to check symmetry
Write a function called isSymmetric that takes two pointers to TreeNode named t1 and t2 and returns a bool. It should return true if both trees are symmetric (mirror images), otherwise false. Use recursion to compare nodes. Then, inside main, set result to the call isSymmetric(root1, root2).
DSA Go
Hint
Check if both nodes are nil, then if one is nil, then if values differ. Recursively check left of one with right of other and vice versa.
4
Print the result
Print the value of the boolean variable result inside the main function.
DSA Go
Hint
Use fmt.Println(result) to print the boolean value.
Practice
(1/5)
1. What does it mean for two binary trees to be symmetric?
easy
A. They are mirror images of each other around their center.
B. They have the same number of nodes.
C. They have the same root value only.
D. They have identical left subtrees only.
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 A
Quick Check:
Symmetry means mirror image [OK]
Hint: Symmetric trees mirror each other exactly [OK]
Common Mistakes:
Thinking same number of nodes means symmetric
Only comparing root values
Ignoring structure differences
2. Which Go function signature correctly represents a function to check if two trees are symmetric?
easy
A. func isSymmetric(t1 *TreeNode) bool
B. func isSymmetric(t1 TreeNode, t2 TreeNode) int
C. func isSymmetric(t1 *TreeNode, t2 *TreeNode) bool
D. func isSymmetric(t1 *TreeNode, t2 *TreeNode) string
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 C
Quick Check:
Pointer params and bool return [OK]
Hint: Use pointers and bool return for tree comparison [OK]
Common Mistakes:
Using value instead of pointer for trees
Returning int or string instead of bool
Missing one tree parameter
3. What will be the output of the following Go code snippet?
D. The recursive calls compare wrong child nodes for symmetry.
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 D
Quick Check:
Mirror children must be compared [OK]
Hint: Compare left with right child for symmetry [OK]
Common Mistakes:
Comparing left with left and right with right
Missing nil base cases
Returning wrong type
5. Given two binary trees, write a Go function to check if they are symmetric. Which approach correctly handles the edge case where one tree is nil and the other is not?
hard
A. Return false immediately if one tree is nil and the other is not.
B. Return true if one tree is nil and the other is not.
C. Ignore nil checks and compare node values only.
D. Return true if both trees have the same number of nodes.
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 A
Quick Check:
Nil mismatch means not symmetric [OK]
Hint: Check nil mismatch first to avoid errors [OK]
Common Mistakes:
Returning true when one tree is nil
Skipping nil checks causing runtime errors
Comparing only node counts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.