What does it mean for two binary trees to be symmetric?
Two binary trees are symmetric if one is a mirror reflection of the other. This means the left subtree of one tree is the mirror of the right subtree of the other tree, and vice versa.
Click to reveal answer
beginner
What is the base case when checking if two trees are symmetric?
If both trees are empty (nil), they are symmetric. If only one is empty, they are not symmetric.
Click to reveal answer
beginner
In Go, how do you define a simple binary tree node struct?
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
Click to reveal answer
intermediate
What recursive checks are done to confirm two trees are symmetric?
Check if the current nodes have the same value, then recursively check if the left subtree of the first tree is symmetric with the right subtree of the second tree, and the right subtree of the first tree is symmetric with the left subtree of the second tree.
Click to reveal answer
intermediate
Why is recursion a good approach to check tree symmetry?
Because symmetry involves comparing subtrees in a mirrored way, recursion naturally allows checking smaller parts of the trees step-by-step until the base cases are reached.
Click to reveal answer
What should you return if both input trees are nil when checking symmetry?
Atrue
Bfalse
Cnil
Derror
✗ Incorrect
If both trees are empty, they are symmetric by definition.
Which pairs of subtrees do you compare to check symmetry?
ALeft subtree of first tree with left subtree of second tree
BRight subtree of first tree with right subtree of second tree
CRoot nodes only
DLeft subtree of first tree with right subtree of second tree
✗ Incorrect
Symmetry means left subtree of one tree matches right subtree of the other tree.
What is the first step in the recursive function to check symmetry?
ACompare the values of the current nodes
BCheck if both nodes are nil
CTraverse left subtree
DTraverse right subtree
✗ Incorrect
First check if both nodes are nil to handle base case.
If one node is nil and the other is not, what should the function return?
Acontinue recursion
Btrue
Cfalse
Dpanic
✗ Incorrect
Trees are not symmetric if one node is missing and the other is present.
What Go data type is used to represent a tree node pointer?
A*TreeNode
Bint
CTreeNode
Dinterface{}
✗ Incorrect
Pointers to TreeNode struct are used to link nodes in Go.
Explain how you would check if two binary trees are symmetric using recursion.
Think about mirror images and how left and right subtrees relate.
You got /4 concepts.
Write the Go struct definition for a binary tree node and describe its fields.
Use type TreeNode struct with int and pointers.
You got /3 concepts.
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.