0
0
DSA Goprogramming~30 mins

Check if Two Trees are Symmetric in DSA Go - Build from Scratch

Choose your learning style9 modes available
Check if Two Trees are Symmetric
📖 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.