0
0
DSA C++programming~10 mins

Check if Two Trees are Symmetric in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Check if Two Trees are Symmetric
Start with two root nodes
Check if both nodes are NULL?
YesReturn True
No
Check if one node is NULL?
YesReturn False
No
Check if node values are equal?
NoReturn False
Yes
Recursively check left subtree of first with right subtree of second
Recursively check right subtree of first with left subtree of second
Return True if both recursive checks are True
End
The process compares two trees node-by-node, checking if they are mirror images by recursively comparing opposite children.
Execution Sample
DSA C++
bool isSymmetric(TreeNode* t1, TreeNode* t2) {
  if (!t1 && !t2) return true;
  if (!t1 || !t2) return false;
  if (t1->val != t2->val) return false;
  return isSymmetric(t1->left, t2->right) && isSymmetric(t1->right, t2->left);
}
Checks if two trees are mirror images by comparing nodes and their opposite children recursively.
Execution Table
Stept1 Node Valuet2 Node ValueCondition CheckedResultAction Taken
111Both NULL?NoCheck if one NULL
211One NULL?NoCheck values equal
311Values equal?YesRecurse left-right and right-left
422Both NULL?NoCheck one NULL
522One NULL?NoCheck values equal
622Values equal?YesRecurse left-right and right-left
7NULLNULLBoth NULL?YesReturn True
8NULLNULLBoth NULL?YesReturn True
933Both NULL?NoCheck one NULL
1033One NULL?NoCheck values equal
1133Values equal?YesRecurse left-right and right-left
12NULLNULLBoth NULL?YesReturn True
13NULLNULLBoth NULL?YesReturn True
14Return from left-right and right-leftTrue && TrueReturn True
15Return from left-right and right-leftTrue && TrueReturn True
16Return from root callTrue && TrueTrees are symmetric
17EndStop
💡 All corresponding nodes matched as mirror images, recursion ends with True
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 11Final
t1 NodeRoot(1)Left(2)Left NULLRight(3)N/A
t2 NodeRoot(1)Right(2)Right NULLLeft(3)N/A
Return ValueN/APendingPendingPendingTrue
Key Moments - 3 Insights
Why do we check if both nodes are NULL first?
Because if both nodes are NULL, it means the subtrees are symmetric at this branch (see steps 7,8,12,13 in execution_table). This is the base case that stops recursion.
Why do we compare t1->left with t2->right and t1->right with t2->left?
Because symmetry means one tree's left subtree mirrors the other's right subtree (see step 3 and recursive calls in execution_table). This ensures mirror structure.
What happens if node values are not equal?
The function returns false immediately (step 6), because symmetric trees must have matching node values at mirrored positions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the condition 'Both NULL?' at step 7?
ANo
BTrue
CYes
DFalse
💡 Hint
Check the 'Condition Checked' and 'Result' columns at step 7 in execution_table
At which step does the function return false due to node value mismatch?
ANo step returns false in this trace
BStep 11
CStep 6
DStep 2
💡 Hint
Look at the 'Action Taken' column in execution_table; no false return is shown
If t1->left was NULL but t2->right was not NULL at step 4, what would happen?
AReturn true
BReturn false
CContinue recursion
DIgnore and check values
💡 Hint
Refer to step 2 and 4 where one NULL node causes false return
Concept Snapshot
Check if two trees are symmetric by:
- Comparing root nodes
- Returning true if both NULL
- Returning false if one NULL
- Checking node values equality
- Recursively comparing left subtree of one with right subtree of other
- Return true only if all checks pass
Full Transcript
This concept checks if two trees are mirror images. We start by comparing the root nodes. If both nodes are NULL, they are symmetric here, so return true. If only one is NULL, return false because symmetry breaks. If node values differ, return false. Otherwise, recursively check the left subtree of the first tree against the right subtree of the second, and the right subtree of the first against the left subtree of the second. If both recursive checks return true, the trees are symmetric. The execution table shows step-by-step how nodes are compared and recursion proceeds until all nodes are checked or a mismatch is found.