What does it mean for two binary trees to be symmetric?
Two binary trees are symmetric if one is the mirror image of the other. This means their structures are mirror opposites and corresponding nodes have the same values.
Click to reveal answer
beginner
What is the base case when checking if two trees are symmetric?
If both nodes are null, they are symmetric at that point. If only one is null, they are not symmetric.
Click to reveal answer
intermediate
In checking symmetry, which pairs of nodes do we compare recursively?
We compare the left child of the first tree with the right child of the second tree, and the right child of the first tree with the left child of the second tree.
Click to reveal answer
intermediate
Why is recursion a good approach to check if two trees are symmetric?
Because symmetry involves comparing subtrees in a mirrored way, recursion naturally handles this by breaking the problem into smaller symmetric checks on child nodes.
Click to reveal answer
intermediate
What is the time complexity of checking if two binary trees are symmetric?
The time complexity is O(n), where n is the number of nodes, because each node is visited once during the comparison.
Click to reveal answer
What should you return if both nodes being compared are null?
ACheck children
BFalse
CTrue
DReturn null
✗ Incorrect
If both nodes are null, they are symmetric at that point, so return true.
Which pairs of nodes are compared to check symmetry?
ALeft with left, right with right
BLeft with right, right with left
CRoot with root only
DRandom pairs
✗ Incorrect
Symmetry means left subtree of one tree matches right subtree of the other and vice versa.
If one node is null and the other is not, what is the result?
ATrees are symmetric
BCheck further nodes
CIgnore null nodes
DTrees are not symmetric
✗ Incorrect
One null and one non-null node means the trees differ in structure, so not symmetric.
What is the main advantage of using recursion here?
AIt simplifies checking mirrored subtrees
BIt speeds up the process
CIt uses less memory
DIt avoids loops
✗ Incorrect
Recursion naturally handles the mirrored structure by calling the function on child nodes in opposite order.
What is the time complexity of checking symmetry for two trees with n nodes?
AO(n)
BO(1)
CO(log n)
DO(n^2)
✗ Incorrect
Each node is visited once, so the time complexity is linear in the number of nodes.
Explain how to check if two binary trees are symmetric using recursion.
Think about comparing left subtree of one tree with right subtree of the other.
You got /4 concepts.
Describe the importance of node value comparison and structure in checking tree symmetry.
Symmetry is about both shape and values matching in a mirrored way.
You got /4 concepts.
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
Step 1: Understand symmetry in trees
Two trees are symmetric if one is the mirror reflection of the other around the center.
Step 2: Compare structure and node values
Both structure and node values must match in a mirrored way for symmetry.
Final Answer:
They are mirror images of each other around their center. -> Option C
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
Step 1: Identify correct return type and parameters
The method should return boolean and accept two TreeNode parameters for two trees.
Step 2: Check method signature syntax
Public access, boolean return, and parameter types match in public boolean isSymmetric(TreeNode root1, TreeNode root2).
Final Answer:
public boolean isSymmetric(TreeNode root1, TreeNode root2) -> Option A
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
Step 1: Analyze tree structure and values
root1 has left=2, right=3; root2 has left=3, right=2, which are mirror images.
Step 2: Trace recursive calls
Values match and recursive calls check mirrored children, all return true.
Final Answer:
true -> Option D
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:
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
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.
Step 2: Identify correct mirrored comparison
It should compare t1.left with t2.right and t1.right with t2.left for symmetry.
Final Answer:
It compares left with left and right with right instead of mirrored children. -> Option A
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
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.
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.
Final Answer:
Recursively check if left subtree of first tree is mirror of right subtree of second tree and vice versa. -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.