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 tree check function
What is the output of the following JavaScript code that checks if two binary trees are symmetric?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function isSymmetric(t1, t2) { if (!t1 && !t2) return true; if (!t1 || !t2) return false; if (t1.val !== t2.val) return false; return isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left); } const treeA = new TreeNode(1, new TreeNode(2), new TreeNode(3)); const treeB = new TreeNode(1, new TreeNode(3), new TreeNode(2)); console.log(isSymmetric(treeA, treeB));
Attempts:
2 left
💡 Hint
Check if the left subtree of one tree matches the right subtree of the other and vice versa.
✗ Incorrect
The function checks if two trees are mirror images. Here, treeA's left child (2) does not match treeB's right child (2) in mirrored positions because treeA's left child is 2 and treeB's right child is 2, but treeA's right child is 3 and treeB's left child is 3, so the values do not match in mirrored positions. The function returns false.
❓ Predict Output
intermediate2:00remaining
Output when one tree is null
What is the output of the code below when checking symmetry between a non-empty tree and a null tree?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function isSymmetric(t1, t2) { if (!t1 && !t2) return true; if (!t1 || !t2) return false; if (t1.val !== t2.val) return false; return isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left); } const treeA = new TreeNode(1); const treeB = null; console.log(isSymmetric(treeA, treeB));
Attempts:
2 left
💡 Hint
If one tree is null and the other is not, they cannot be symmetric.
✗ Incorrect
The function returns false because one tree exists and the other is null, so they cannot be symmetric.
🔧 Debug
advanced2:00remaining
Identify the output of a symmetric tree check with nested nodes
What is the output of this code that checks if two trees with nested nodes are symmetric?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function isSymmetric(t1, t2) { if (!t1 && !t2) return true; if (!t1 || !t2) return false; if (t1.val !== t2.val) return false; return isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left); } const treeA = new TreeNode(1, new TreeNode(2, new TreeNode(3), null), new TreeNode(4)); const treeB = new TreeNode(1, new TreeNode(4), new TreeNode(2, null, new TreeNode(3))); console.log(isSymmetric(treeA, treeB));
Attempts:
2 left
💡 Hint
Check if the left subtree of one tree matches the right subtree of the other recursively.
✗ Incorrect
The trees are mirror images: treeA's left subtree has 2 with left child 3, and treeB's right subtree has 2 with right child 3. The right subtree of treeA is 4 and matches the left subtree of treeB which is 4. So the function returns true.
🧠 Conceptual
advanced1:30remaining
Understanding the base cases in symmetric tree check
In the function to check if two trees are symmetric, why do we return true when both nodes are null and false when only one is null?
Attempts:
2 left
💡 Hint
Think about what it means for two subtrees to be mirrors when they are empty or partially empty.
✗ Incorrect
Two null nodes mean both subtrees ended at the same time, so they are symmetric. If only one node is null, the other subtree still has nodes, so they are not symmetric.
🚀 Application
expert2:30remaining
Number of symmetric subtree pairs in two trees
Given two binary trees, how many pairs of nodes (one from each tree) form symmetric subtrees? Consider the code below and determine the output.
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function isSymmetric(t1, t2) { if (!t1 && !t2) return true; if (!t1 || !t2) return false; if (t1.val !== t2.val) return false; return isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left); } function countSymmetricPairs(t1, t2) { if (!t1 && !t2) return 0; if (!t1 || !t2) return 0; let count = 0; if (isSymmetric(t1, t2)) count = 1; return count + countSymmetricPairs(t1.left, t2.right) + countSymmetricPairs(t1.right, t2.left); } const treeA = new TreeNode(1, new TreeNode(2, new TreeNode(3), null), new TreeNode(4)); const treeB = new TreeNode(1, new TreeNode(4), new TreeNode(2, null, new TreeNode(3))); console.log(countSymmetricPairs(treeA, treeB));
Attempts:
2 left
💡 Hint
Count each pair of nodes that form symmetric subtrees including leaf nodes.
✗ Incorrect
The pairs are: (root nodes 1-1), (left subtree 2 and right subtree 2), (right subtree 4 and left subtree 4), (left-left 3 and right-right 3). The leaf node pairs count as symmetric subtrees too. Total is 4.