0
0
DSA Javascriptprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Check if Two Trees are Symmetric
Start with root nodes of both trees
Compare values of current nodes
Return false - trees not symmetric
Recursively check left of Tree1 with right of Tree2
Recursively check right of Tree1 with left of Tree2
Both recursive calls true?
Yes
Return true - trees are symmetric
Start by comparing root nodes, then recursively check if left subtree of one tree matches right subtree of the other, and vice versa, returning true only if all pairs match.
Execution Sample
DSA Javascript
function isSymmetric(t1, 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);
}
This function checks if two trees are mirror images by comparing nodes recursively.
Execution Table
Stept1 Node Valuet2 Node ValueCondition CheckedResultNext Action
111Both nodes exist and values equal?trueCheck left of t1 with right of t2 and right of t1 with left of t2
222Both nodes exist and values equal?trueCheck left of t1.left with right of t2.right and right of t1.left with left of t2.right
333Both nodes exist and values equal?trueCheck left and right children (both null)
4nullnullBoth nodes null?trueReturn true
5nullnullBoth nodes null?trueReturn true
6nullnullBoth nodes null?trueReturn true
7nullnullBoth nodes null?trueReturn true
822Both nodes exist and values equal?trueCheck left of t1.right with right of t2.left and right of t1.right with left of t2.left
933Both nodes exist and values equal?trueCheck left and right children (both null)
10nullnullBoth nodes null?trueReturn true
11nullnullBoth nodes null?trueReturn true
12nullnullBoth nodes null?trueReturn true
13nullnullBoth nodes null?trueReturn true
14All recursive calls returned truetrueReturn true - trees are symmetric
💡 All corresponding nodes matched or were both null, confirming symmetry.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 8Final
t1 NodeRoot(1)Left(2)Left.Left(3)Right(2)null
t2 NodeRoot(1)Right(2)Right.Right(3)Left(2)null
Resultundefinedtruetruetruetrue
Key Moments - 3 Insights
Why do we compare t1.left with t2.right and t1.right with t2.left instead of matching left with left and right with right?
Because symmetry means one tree is a mirror image of the other, so left subtree of one matches right subtree of the other. See execution_table steps 1 and 2 where this cross comparison happens.
What happens when both nodes are null during recursion?
If both nodes are null, it means the branches ended symmetrically, so we return true. This is shown in execution_table steps 4, 5, 6, 7, 10, 11, 12, and 13.
Why do we return false immediately if one node is null and the other is not?
Because that means the structure is not symmetric at that point. The function returns false early to stop unnecessary checks, as seen in the condition checks in the code and implied in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what nodes are being compared?
At1.left with t2.right
Bt1.left with t2.left
Ct1.right with t2.right
Dt1.right with t2.left
💡 Hint
Check the 'Next Action' column in step 1 and the 't1 Node Value' and 't2 Node Value' columns in step 2.
At which step does the function confirm that both nodes are null and returns true?
AStep 3
BStep 4
CStep 8
DStep 14
💡 Hint
Look for rows where 'Condition Checked' is 'Both nodes null?' and 'Result' is true.
If at step 1 the root node values were different, what would be the result?
AReturn true
BReturn false
CContinue recursion
DIgnore and check children
💡 Hint
Refer to the 'Condition Checked' and 'Result' columns in step 1 of the execution_table.
Concept Snapshot
Check if Two Trees are Symmetric:
- Compare root nodes values.
- Recursively check t1.left with t2.right and t1.right with t2.left.
- If both nodes null, return true.
- If one null or values differ, return false.
- Return true only if all pairs match.
Full Transcript
This concept checks if two trees are mirror images. It starts by comparing the root nodes. If both are null, they are symmetric at that point. If one is null or values differ, they are not symmetric. Otherwise, it recursively compares 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. The process continues until all nodes are checked. If all pairs match, the trees are symmetric.