Bird
Raised Fist0
DSA Pythonprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Check if Two Trees are Symmetric
Start with root nodes of both trees
↓
Check if both nodes are None?
Yes→Return True
No↓
Check if one node is None or values differ?
Yes→Return False
No↓
Recursively check left subtree of first tree with right subtree of second tree
↓
Recursively check right subtree of first tree with left subtree of second tree
↓
Return True if both recursive checks are True, else False
The flow compares nodes of two trees starting from roots, checking symmetry by matching left subtree of one with right subtree of the other recursively.
Execution Sample
DSA Python
def is_symmetric(t1, t2):
    if not t1 and not t2:
        return True
    if not t1 or not t2 or t1.val != t2.val:
        return False
    return is_symmetric(t1.left, t2.right) and is_symmetric(t1.right, t2.left)
This function checks if two trees are mirror images by comparing nodes recursively.
Execution Table
Stept1.valt2.valCondition CheckedResultAction/Return
111Both None? No; One None or values differ? NoTrueRecurse left-right and right-left
222Both None? No; One None or values differ? NoTrueRecurse left-right and right-left
3NoneNoneBoth None? YesTrueReturn True
4NoneNoneBoth None? YesTrueReturn True
533Both None? No; One None or values differ? NoTrueRecurse left-right and right-left
6NoneNoneBoth None? YesTrueReturn True
7NoneNoneBoth None? YesTrueReturn True
8Return TrueReturn TrueCombine recursive resultsTrueReturn True
9Return TrueReturn TrueCombine recursive resultsTrueReturn True
10Return TrueReturn TrueCombine recursive resultsTrueReturn True
💡 All corresponding nodes matched symmetrically, recursion ends with True
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 5Final
t1.val12None3Return True
t2.val12None3Return True
Key Moments - 3 Insights
Why do we check if both nodes are None first?
Because if both nodes are None, it means both subtrees ended symmetrically here, so we return True immediately (see execution_table rows 3 and 4).
What happens if one node is None but the other is not?
We return False because the trees differ in structure at this point, breaking symmetry (refer to execution_table step 2 condition).
Why do we compare t1.left with t2.right and t1.right with t2.left?
Because symmetry means left subtree of one tree mirrors right subtree of the other, so we check these pairs recursively (shown in steps 1, 2, and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the result of the condition 'Both None?'
AFalse
BTrue
CNot checked
DError
💡 Hint
Check the 'Condition Checked' and 'Result' columns at step 3 in execution_table
At which step does the function return True because both nodes are None?
AStep 3
BStep 5
CStep 2
DStep 1
💡 Hint
Look for 'Both None? Yes' in the 'Condition Checked' column in execution_table
If t1.val and t2.val differ at any step, what would the function return?
ATrue
BContinue recursion
CFalse
DError
💡 Hint
Refer to the condition 'One None or values differ? Yes' in execution_table step 2
Concept Snapshot
Check if two trees are symmetric by:
- Returning True if both nodes are None
- Returning False if one node is None or values differ
- Recursively comparing left subtree of first tree with right subtree of second tree and vice versa
- Return True only if both recursive calls return True
Full Transcript
This concept shows how to check if two trees are symmetric by comparing their nodes recursively. We start from the root nodes of both trees. If both nodes are None, we return True because empty trees are symmetric. If one node is None or their values differ, we return False because symmetry breaks. Otherwise, we 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. The function returns True only if both these recursive checks return True. The execution table traces these steps with example node values, showing how the function returns True after all checks pass.

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

  1. Step 1: Understand symmetry in trees

    Two trees are symmetric if one is the mirror reflection of the other around the center.
  2. Step 2: Compare structure and node values

    Both structure and node values must match in a mirrored way for symmetry.
  3. Final Answer:

    They are mirror images of each other around their center. -> Option A
  4. Quick Check:

    Symmetry means mirror image = A [OK]
Hint: Symmetric trees mirror each other exactly [OK]
Common Mistakes:
  • Thinking same number of nodes means symmetric
  • Ignoring node values in comparison
  • Checking only left or right subtree
2. Which of the following Python function signatures is correct to check if two trees are symmetric?
easy
A. def is_symmetric(tree1, tree2)
B. def is_symmetric(tree1 tree2):
C. def is_symmetric(tree1, tree2) -> bool:
D. def is_symmetric(tree1, tree2): return True

Solution

  1. Step 1: Check syntax for function definition

    Python functions require parentheses around parameters and a colon at the end.
  2. Step 2: Identify correct type hint usage

    def is_symmetric(tree1, tree2) -> bool: uses correct syntax with type hint and colon, making it valid and clear.
  3. Final Answer:

    def is_symmetric(tree1, tree2) -> bool: -> Option C
  4. Quick Check:

    Correct Python function syntax with type hint = D [OK]
Hint: Use parentheses and colon; type hints are optional but clear [OK]
Common Mistakes:
  • Missing comma between parameters
  • Omitting colon at function end
  • Incorrect parameter syntax without parentheses
3. What will be the output of the following code snippet?
class Node:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

def is_mirror(t1, t2):
    if not t1 and not t2:
        return True
    if not t1 or not t2:
        return False
    return (t1.val == t2.val) and is_mirror(t1.left, t2.right) and is_mirror(t1.right, t2.left)

root1 = Node(1)
root1.left = Node(2)
root1.right = Node(3)

root2 = Node(1)
root2.left = Node(3)
root2.right = Node(2)

print(is_mirror(root1, root2))
medium
A. True
B. False
C. None
D. Error

Solution

  1. Step 1: Check base cases for empty nodes

    Both trees have root 1, and their left and right children are swapped but equal in value.
  2. Step 2: Recursively verify mirror condition

    Left of root1 (2) matches right of root2 (2), and right of root1 (3) matches left of root2 (3), so all conditions return True.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    Mirror check returns True for swapped children = A [OK]
Hint: Mirror means left of one equals right of other [OK]
Common Mistakes:
  • Confusing left and right children in recursion
  • Ignoring base cases for None nodes
  • Assuming same structure means symmetric
4. Identify the error in this code snippet that checks if two trees are symmetric:
def is_symmetric(t1, t2):
    if t1 is None and t2 is None:
        return True
    if t1 is None or t2 is None:
        return False
    if t1.val != t2.val:
        return False
    return is_symmetric(t1.left, t2.left) and is_symmetric(t1.right, t2.right)
medium
A. It does not check for None nodes.
B. It compares left with left and right with right instead of mirror sides.
C. It returns True too early without checking values.
D. It uses 'is' instead of '==' for value comparison.

Solution

  1. Step 1: Analyze recursive calls for symmetry

    The function compares left subtree of t1 with left subtree of t2 and right with right, which is incorrect for mirror symmetry.
  2. Step 2: Correct mirror comparison

    It should compare left of t1 with right of t2 and right of t1 with left of t2 to check mirror symmetry properly.
  3. Final Answer:

    It compares left with left and right with right instead of mirror sides. -> Option B
  4. Quick Check:

    Mirror check requires cross comparison = B [OK]
Hint: Mirror means cross compare left with right [OK]
Common Mistakes:
  • Comparing same sides instead of mirror sides
  • Confusing 'is' and '==' for values
  • Missing base cases for None nodes
5. Given two binary trees, write a function to check if they are symmetric considering that one tree might be None. Which approach correctly handles this edge case?
hard
A. Return False immediately if either tree is None.
B. Ignore None checks and compare node values only.
C. Only compare root values and ignore subtrees.
D. Return True if both trees are None, else check mirror recursively.

Solution

  1. Step 1: Handle None edge cases carefully

    If both trees are None, they are symmetric by definition, so return True.
  2. Step 2: Recursively check mirror symmetry

    If only one is None, return False. Otherwise, compare root values and recursively check left of one with right of other and vice versa.
  3. Final Answer:

    Return True if both trees are None, else check mirror recursively. -> Option D
  4. Quick Check:

    Both None means symmetric, else recurse mirror check = C [OK]
Hint: Both None means symmetric; one None means not symmetric [OK]
Common Mistakes:
  • Returning False if either tree is None without checking both
  • Skipping recursive mirror checks
  • Comparing only root values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard