Challenge - 5 Problems
Symmetry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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)); } }
Attempts:
2 left
💡 Hint
Think about the symmetry condition: left subtree of one tree must match right subtree of the other.
✗ Incorrect
The trees have the same root and children values but are not mirror images. The left child of tree a is 2 and right child is 3, but for symmetry, a.left must match b.right and a.right must match b.left. Here, a.left=2 and b.right=3 differ, so result is false.
❓ Predict Output
intermediateOutput 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)); } }
Attempts:
2 left
💡 Hint
Check the base cases for null trees in the method.
✗ Incorrect
If one tree is null and the other is not, the method returns false immediately because they cannot be symmetric.
🔧 Debug
advancedIdentify 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)); } }
Attempts:
2 left
💡 Hint
Check the recursive calls for symmetry direction.
✗ Incorrect
The code incorrectly compares left subtree with left subtree and right with right, instead of left with right and right with left. This causes it to return false for symmetric trees.
🧠 Conceptual
advancedUnderstanding the symmetry condition in two trees
Which of the following best describes the condition for two binary trees to be symmetric?
Attempts:
2 left
💡 Hint
Think about mirror images and how left and right subtrees relate.
✗ Incorrect
Symmetry means one tree is a mirror image of the other. This requires the left subtree of one to match the right subtree of the other and node values to be equal.
❓ Predict Output
expertOutput 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)); } }
Attempts:
2 left
💡 Hint
Check carefully the mirror structure and values of the subtrees.
✗ Incorrect
The trees are mirror images with matching node values in corresponding mirror positions, so the method returns true.
