0
0
DSA Typescriptprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Check if Two Trees are Symmetric
Start with root nodes of both trees
Are both nodes null?
YesReturn True
No
Is one node null?
YesReturn False
No
Do node values match?
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 flow compares nodes of two trees step-by-step to check if they are mirror images, returning false on mismatch or null asymmetry.
Execution Sample
DSA Typescript
function isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean {
  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);
}
This function checks if two trees are symmetric by comparing nodes recursively.
Execution Table
Stept1 Node Valuet2 Node ValueCondition CheckedResultAction Taken
111Both null? (No)FalseContinue
211One null? (No)FalseContinue
311Values equal? (Yes)TrueRecurse left-right and right-left
422Both null? (No)FalseContinue
522One null? (No)FalseContinue
622Values equal? (Yes)TrueRecurse left-right and right-left
7nullnullBoth null? (Yes)TrueReturn True
8nullnullBoth null? (Yes)TrueReturn True
933Both null? (No)FalseContinue
1033One null? (No)FalseContinue
1133Values equal? (Yes)TrueRecurse left-right and right-left
12nullnullBoth null? (Yes)TrueReturn True
13nullnullBoth null? (Yes)TrueReturn True
14Return from recursionTrueCombine results
15Return from recursionTrueCombine results
16Return from recursionTrueFinal result: Trees are symmetric
💡 All node pairs matched symmetrically or were both null, so function returns True.
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 11Final
t1 NodeRoot(1)Left(2)Left nullRight(3)null
t2 NodeRoot(1)Right(2)Right nullLeft(3)null
Resultundefinedundefinedundefinedundefinedtrue
Key Moments - 3 Insights
Why do we check if both nodes are null first?
Because if both nodes are null, it means this part of the trees matches perfectly (see steps 7, 8, 12, 13 in execution_table). We return true immediately to stop further checks.
What happens if one node is null but the other is not?
This means the trees are not symmetric at this point (see step 2 in execution_table). We return false immediately because one side has a node while the other does not.
Why do we compare t1.left with t2.right and t1.right with t2.left?
Because symmetry means one tree is a mirror image of the other. So left subtree of one matches right subtree of the other (see step 3 and recursive calls).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what is the condition checked and its result?
AValues are unequal, result False
BBoth nodes are null, result True
COne node is null, result False
DValues are equal, result True
💡 Hint
Check the 'Condition Checked' and 'Result' columns at step 7 in execution_table.
At which step does the function confirm the final result that trees are symmetric?
AStep 16
BStep 14
CStep 11
DStep 6
💡 Hint
Look for the step with 'Final result' in the 'Action Taken' column in execution_table.
If t1.left was null but t2.right was not, what would happen in the execution?
AReturn true immediately
BContinue recursion
CReturn false immediately
DIgnore and check other nodes
💡 Hint
Refer to the key moment about one node null and the other not, and step 2 in execution_table.
Concept Snapshot
Check if Two Trees are Symmetric:
- Compare two nodes at a time.
- If both null, return true.
- If one null, return false.
- If values differ, return false.
- 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 symmetric by comparing nodes recursively. We start by checking if both nodes are null, which means symmetry at that point. If only one node is null, symmetry breaks and we return false. If both nodes exist, we compare their values. If values differ, return false. Otherwise, we recursively check the left subtree of the first tree with the right subtree of the second tree, and the right subtree of the first tree with the left subtree of the second tree. If all these checks return true, the trees are symmetric. The execution table traces these steps with node values and conditions checked. Variable tracker shows how nodes and results change during recursion. Key moments clarify common confusions about null checks and recursive comparisons. The visual quiz tests understanding of these steps. The snapshot summarizes the approach in simple rules.