Bird
Raised Fist0
DSA Javaprogramming~5 mins

Check if Two Trees are Symmetric in DSA Java - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Check if Two Trees are Symmetric
O(n)
Understanding Time Complexity

We want to know how the time needed to check if two trees are mirror images grows as the trees get bigger.

How does the work increase when the trees have more nodes?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


public boolean isSymmetric(TreeNode root) {
    if (root == null) return true;
    return isMirror(root.left, root.right);
}

private 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);
}
    

This code checks if a tree is symmetric by comparing its left and right subtrees recursively.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Recursive calls comparing pairs of nodes from left and right subtrees.
  • How many times: Once for each node in the tree, visiting all nodes to check symmetry.
How Execution Grows With Input

As the tree grows, the function checks each node once by comparing pairs of nodes.

Input Size (n)Approx. Operations
10About 10 comparisons
100About 100 comparisons
1000About 1000 comparisons

Pattern observation: The work grows roughly in direct proportion to the number of nodes.

Final Time Complexity

Time Complexity: O(n)

This means the time to check symmetry grows linearly with the number of nodes in the tree.

Common Mistake

[X] Wrong: "The function only checks half the nodes, so it runs in half the time."

[OK] Correct: Even though it compares pairs, it still visits every node once, so the time grows with all nodes.

Interview Connect

Understanding how recursive tree checks scale helps you explain your approach clearly and confidently in interviews.

Self-Check

"What if the tree is not balanced? How would the time complexity change?"

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