Challenge - 5 Problems
Symmetry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of symmetric check on two trees
What is the output of the following TypeScript code that checks if two binary trees are symmetric?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } function isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean { if (!t1 && !t2) return true; if (!t1 || !t2) return false; return t1.val === t2.val && isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left); } const treeA = new TreeNode(1, new TreeNode(2, null, new TreeNode(3)), new TreeNode(2, null, new TreeNode(3))); const treeB = new TreeNode(1, new TreeNode(2, null, new TreeNode(3)), new TreeNode(2, new TreeNode(3), null)); console.log(isSymmetric(treeA, treeB));
Attempts:
2 left
💡 Hint
Check if the left subtree of one tree matches the right subtree of the other tree recursively.
✗ Incorrect
The function checks if two trees are mirror images. treeA and treeB differ in structure on the right subtree of the root's children, so they are not symmetric.
🧠 Conceptual
intermediate1:30remaining
Understanding symmetry in binary trees
Which of the following best describes when two binary trees are symmetric?
Attempts:
2 left
💡 Hint
Symmetry means one tree looks like the mirror reflection of the other.
✗ Incorrect
Two trees are symmetric if one is the mirror image of the other, meaning the left subtree of one matches the right subtree of the other and vice versa, with equal node values.
🔧 Debug
advanced2:00remaining
Identify the error in symmetric tree check
What error will this TypeScript code produce when checking if two trees are symmetric?
DSA Typescript
function isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return t1.val === t2.val && isSymmetric(t1.left, t2.left) && isSymmetric(t1.right, t2.right);
}Attempts:
2 left
💡 Hint
Check if the recursive calls compare the correct subtrees for symmetry.
✗ Incorrect
The code compares left subtree with left subtree and right with right, which checks equality but not symmetry (mirror). It will return false for symmetric trees.
❓ Predict Output
advanced1:00remaining
Output of symmetric check with null trees
What is the output of this TypeScript code when both trees are null?
DSA Typescript
function isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return t1.val === t2.val && isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}
console.log(isSymmetric(null, null));Attempts:
2 left
💡 Hint
Two empty trees are symmetric by definition.
✗ Incorrect
If both trees are null, the function returns true immediately as they are symmetric (empty trees).
🚀 Application
expert3:00remaining
Number of symmetric subtree pairs in two trees
Given two binary trees, how many pairs of subtrees (one from each tree) are symmetric according to the isSymmetric function below?
Consider these trees:
Tree1:
1
/ \
2 3
/ \
4 5
Tree2:
1
/ \
3 2
/ \
5 4
Use the isSymmetric function:
function isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return t1.val === t2.val && isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}
Attempts:
2 left
💡 Hint
Count all subtree pairs including the root and leaf nodes that are mirror images.
✗ Incorrect
The symmetric subtree pairs are:
- Leaf nodes 4 and 4
- Leaf nodes 5 and 5
- Subtrees rooted at 2 and 2
- Subtrees rooted at 3 and 3
- The whole trees rooted at 1 and 1
Total 5 unique pairs.