0
0
DSA Goprogramming~10 mins

Check if Two Trees are Symmetric in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - Check if Two Trees are Symmetric
Start with two root nodes
Are both nodes nil?
YesReturn True
No
Is one node nil?
YesReturn False
No
Do nodes have same value?
NoReturn False
Yes
Check left subtree of first with right subtree of second
Check right subtree of first with left subtree of second
Return True if both checks True, else False
The function compares two trees node-by-node to check if they are mirror images, returning true only if all corresponding nodes match symmetrically.
Execution Sample
DSA Go
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 symmetric by recursively comparing nodes in mirrored positions.
Execution Table
Stept1.Valt2.ValCondition CheckedResultNext Action
111Both nil? NoContinueCheck values
211One nil? NoContinueCompare values
311Values equal? YesContinueCheck left-right and right-left subtrees
422Both nil? NoContinueCheck values
522One nil? NoContinueCompare values
622Values equal? YesContinueCheck left-right and right-left subtrees
7nilnilBoth nil? YesReturn TrueBack to previous call
8nilnilBoth nil? YesReturn TrueBack to previous call
933Both nil? NoContinueCheck values
1033One nil? NoContinueCompare values
1133Values equal? YesContinueCheck left-right and right-left subtrees
12nilnilBoth nil? YesReturn TrueBack to previous call
13nilnilBoth nil? YesReturn TrueBack to previous call
14All recursive calls returned TrueReturn TrueTrees are symmetric
15ExitAll checks passed
💡 All corresponding nodes matched symmetrically, so the trees are symmetric.
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 11Final
t1.Val1233N/A
t2.Val1233N/A
Return ValueN/AN/AN/AN/ATrue
Key Moments - 3 Insights
Why do we check if both nodes are nil first?
Because if both nodes are nil, it means the subtrees are empty and symmetric at this point (see steps 7, 8, 12, 13 in execution_table).
What happens if one node is nil but the other is not?
The trees are not symmetric because one side has a node while the other does not (see step 5 condition 'One nil? No' means if it was Yes, return false).
Why do we compare t1.Left with t2.Right and t1.Right with t2.Left?
Because symmetry means the left subtree of one tree mirrors the right subtree of the other, so we check these pairs recursively (see step 3 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the condition 'Both nil?' at step 7?
ANot checked
BFalse
CTrue
DError
💡 Hint
Check the 'Condition Checked' and 'Result' columns at step 7 in execution_table.
At which step does the function confirm that node values are equal for the first time?
AStep 3
BStep 1
CStep 6
DStep 10
💡 Hint
Look for 'Values equal? Yes' in the 'Condition Checked' column in execution_table.
If t1.Left was nil but t2.Right was not, what would happen in the execution?
AReturn True
BReturn False
CContinue checking
DCause an error
💡 Hint
Refer to the key moment about one node being nil and the other not, and the condition 'One nil?' in execution_table.
Concept Snapshot
Check if Two Trees are Symmetric:
- Compare two nodes: if both nil, return true
- If one nil, return false
- Check if values equal
- Recursively check left of one with right of other and vice versa
- Return true only if all checks pass
Full Transcript
This concept shows how to check if two trees are mirror images. We start by comparing the root nodes. If both are empty, they are symmetric here. If only one is empty, they are not symmetric. If both have values, we check if the values match. Then we recursively check the left subtree of the first tree against the right subtree of the second tree, and the right subtree of the first tree against the left subtree of the second tree. If all these checks return true, the trees are symmetric. The execution table traces these steps carefully, showing conditions and results at each step.