Bird
Raised Fist0
DSA Javaprogramming~3 mins

Why Check if Two Trees are Symmetric in DSA Java?

Choose your learning style10 modes available
The Big Idea

What if you could instantly tell if two complex trees are perfect mirror images without getting lost in details?

The Scenario

Imagine you have two family trees drawn on paper, and you want to see if one is a mirror image of the other. Doing this by looking at every branch and leaf manually is tiring and confusing.

The Problem

Manually comparing each node and its position is slow and easy to mess up. You might miss a branch or compare nodes in the wrong order, leading to wrong conclusions.

The Solution

Using a smart method to check symmetry lets the computer compare both trees step-by-step, matching nodes on opposite sides automatically and quickly.

Before vs After
✗ Before
boolean isSymmetric(TreeNode t1, TreeNode t2) {
  // manually check each node and position
  // complex and error-prone
  return false;
}
✓ After
boolean isSymmetric(TreeNode root) {
  return isMirror(root, root);
}
boolean isMirror(TreeNode t1, TreeNode t2) {
  if (t1 == null && t2 == null) return true;
  if (t1 == null || t2 == null) return false;
  return (t1.val == t2.val) && isMirror(t1.left, t2.right) && isMirror(t1.right, t2.left);
}
What It Enables

This lets you quickly and reliably check if two trees are mirror images, which is useful in many tree-based problems.

Real Life Example

Checking if two organizational charts are symmetric can help spot errors or confirm balanced team structures.

Key Takeaways

Manual comparison is slow and error-prone.

Recursive checking matches nodes on opposite sides automatically.

Enables fast and reliable symmetry checks in trees.

Practice

(1/5)
1. What does it mean for two binary trees to be symmetric?
easy
A. They have identical left subtrees only.
B. They have the same number of nodes.
C. They are mirror images of each other around their center.
D. They have the same root value 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 C
  4. Quick Check:

    Symmetry means mirror image = D [OK]
Hint: Symmetric trees mirror each other exactly [OK]
Common Mistakes:
  • Thinking same number of nodes means symmetric
  • Only comparing root values
  • Ignoring structure differences
2. Which Java method signature correctly defines a function to check if two trees are symmetric?
easy
A. public boolean isSymmetric(TreeNode root1, TreeNode root2)
B. boolean isSymmetric(TreeNode root1, TreeNode root2) {}
C. public void isSymmetric(TreeNode root1, TreeNode root2)
D. public boolean isSymmetric(TreeNode root)

Solution

  1. Step 1: Identify correct return type and parameters

    The method should return boolean and accept two TreeNode parameters for two trees.
  2. Step 2: Check method signature syntax

    Public access, boolean return, and parameter types match in public boolean isSymmetric(TreeNode root1, TreeNode root2).
  3. Final Answer:

    public boolean isSymmetric(TreeNode root1, TreeNode root2) -> Option A
  4. Quick Check:

    Correct signature returns boolean with two TreeNode inputs [OK]
Hint: Boolean return with two TreeNode inputs is needed [OK]
Common Mistakes:
  • Using void return type
  • Missing public keyword
  • Using only one TreeNode parameter
3. What will be the output of the following code snippet?
TreeNode root1 = new TreeNode(1);
root1.left = new TreeNode(2);
root1.right = new TreeNode(3);
TreeNode root2 = new TreeNode(1);
root2.left = new TreeNode(3);
root2.right = new TreeNode(2);

System.out.println(isSymmetric(root1, root2));

public static boolean isSymmetric(TreeNode t1, TreeNode t2) {
    if (t1 == null && t2 == null) return true;
    if (t1 == null || t2 == null) return false;
    return (t1.val == t2.val) && isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}
medium
A. Runtime exception
B. false
C. Compilation error
D. true

Solution

  1. Step 1: Analyze tree structure and values

    root1 has left=2, right=3; root2 has left=3, right=2, which are mirror images.
  2. Step 2: Trace recursive calls

    Values match and recursive calls check mirrored children, all return true.
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    Mirror structure and values = true [OK]
Hint: Check mirrored nodes and values recursively [OK]
Common Mistakes:
  • Ignoring mirrored child comparison
  • Confusing left and right children
  • Assuming same structure means false
4. Identify the bug in this code snippet that checks if two trees are symmetric:
public boolean isSymmetric(TreeNode t1, TreeNode t2) {
    if (t1 == null && t2 == null) return true;
    if (t1 == null || t2 == null) return false;
    return (t1.val == t2.val) && isSymmetric(t1.left, t2.left) && isSymmetric(t1.right, t2.right);
}
medium
A. It compares left with left and right with right instead of mirrored children.
B. It does not check for null nodes.
C. It returns true when both nodes are null.
D. It uses incorrect return type.

Solution

  1. Step 1: Review recursive calls for symmetry

    The code compares t1.left with t2.left and t1.right with t2.right, which checks identical structure, not mirror.
  2. Step 2: Identify correct mirrored comparison

    It should compare t1.left with t2.right and t1.right with t2.left for symmetry.
  3. Final Answer:

    It compares left with left and right with right instead of mirrored children. -> Option A
  4. Quick Check:

    Mirror check needs cross child comparison [OK]
Hint: Mirror check swaps left and right children [OK]
Common Mistakes:
  • Comparing same side children instead of mirrored
  • Ignoring null checks
  • Wrong return type usage
5. Given two binary trees, write a Java method to check if they are symmetric. Which approach correctly solves this problem? A) Traverse both trees in preorder and compare node values in order. B) Recursively check if left subtree of first tree is mirror of right subtree of second tree and vice versa. C) Compare the height of both trees and return true if equal. D) Check if both trees have the same number of leaf nodes.
hard
A. Traverse both trees in preorder and compare node values in order.
B. Recursively check if left subtree of first tree is mirror of right subtree of second tree and vice versa.
C. Compare the height of both trees and return true if equal.
D. Check if both trees have the same number of leaf nodes.

Solution

  1. Step 1: Understand symmetry check requirements

    Symmetry means one tree is mirror image of the other, so left subtree of one matches right subtree of other.
  2. Step 2: Evaluate approaches

    Recursively check if left subtree of first tree is mirror of right subtree of second tree and vice versa. correctly uses recursive mirror checks; others ignore structure or order.
  3. Final Answer:

    Recursively check if left subtree of first tree is mirror of right subtree of second tree and vice versa. -> Option B
  4. Quick Check:

    Mirror recursive check solves symmetry [OK]
Hint: Mirror recursive check is key to symmetry [OK]
Common Mistakes:
  • Using preorder traversal comparison
  • Comparing only height or leaf counts
  • Ignoring mirrored subtree checks
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