Bird
Raised Fist0
DSA Javaprogramming~20 mins

Check if Two Trees are Symmetric in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Symmetry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of symmetric tree check for two identical trees
What is the output of the following Java code that checks if two binary trees are symmetric?
DSA Java
class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int val) { this.val = val; }
}

public class SymmetricCheck {
    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);
    }

    public static void main(String[] args) {
        TreeNode a = new TreeNode(1);
        a.left = new TreeNode(2);
        a.right = new TreeNode(3);

        TreeNode b = new TreeNode(1);
        b.left = new TreeNode(2);
        b.right = new TreeNode(3);

        System.out.println(isSymmetric(a, b));
    }
}
Afalse
BRuntime exception
CCompilation error
Dtrue
Attempts:
2 left
💡 Hint
Think about the symmetry condition: left subtree of one tree must match right subtree of the other.
❓ Predict Output
intermediate
2:00remaining
Output when one tree is null and the other is not
What will the output be when checking symmetry between a null tree and a non-null tree?
DSA Java
class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int val) { this.val = val; }
}

public class SymmetricCheck {
    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);
    }

    public static void main(String[] args) {
        TreeNode a = null;
        TreeNode b = new TreeNode(1);
        System.out.println(isSymmetric(a, b));
    }
}
Atrue
BNullPointerException
Cfalse
DCompilation error
Attempts:
2 left
💡 Hint
Check the base cases for null trees in the method.
🔧 Debug
advanced
2:00remaining
Identify the error in this symmetric tree check code
What error will this code produce when checking if two trees are symmetric?
DSA Java
class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int val) { this.val = val; }
}

public class SymmetricCheck {
    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.left) && isSymmetric(t1.right, t2.right);
    }

    public static void main(String[] args) {
        TreeNode a = new TreeNode(1);
        a.left = new TreeNode(2);
        a.right = new TreeNode(3);

        TreeNode b = new TreeNode(1);
        b.left = new TreeNode(3);
        b.right = new TreeNode(2);

        System.out.println(isSymmetric(a, b));
    }
}
AStackOverflowError
Btrue
CCompilation error
Dfalse
Attempts:
2 left
💡 Hint
Check the recursive calls for symmetry direction.
🧠 Conceptual
advanced
2:00remaining
Understanding the symmetry condition in two trees
Which of the following best describes the condition for two binary trees to be symmetric?
ABoth trees have the same structure and node values in the same positions.
BThe left subtree of the first tree is a mirror image of the right subtree of the second tree and vice versa, with matching node values.
CBoth trees have the same number of nodes regardless of structure or values.
DThe root nodes have the same value, but subtrees can differ.
Attempts:
2 left
💡 Hint
Think about mirror images and how left and right subtrees relate.
❓ Predict Output
expert
2:00remaining
Output of symmetric check on complex trees
What is the output of the following code that checks if two complex trees are symmetric?
DSA Java
class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int val) { this.val = val; }
}

public class SymmetricCheck {
    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);
    }

    public static void main(String[] args) {
        TreeNode a = new TreeNode(1);
        a.left = new TreeNode(2);
        a.right = new TreeNode(2);
        a.left.left = new TreeNode(3);
        a.left.right = new TreeNode(4);
        a.right.left = new TreeNode(4);
        a.right.right = new TreeNode(3);

        TreeNode b = new TreeNode(1);
        b.left = new TreeNode(2);
        b.right = new TreeNode(2);
        b.left.left = new TreeNode(3);
        b.left.right = new TreeNode(4);
        b.right.left = new TreeNode(4);
        b.right.right = new TreeNode(3);

        System.out.println(isSymmetric(a, b));
    }
}
Atrue
Bfalse
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Check carefully the mirror structure and values of the subtrees.

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