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 for two trees
What is the output of the following C++ code that checks if two binary trees are symmetric?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
bool isSymmetric(TreeNode* root) {
if (!root) return true;
return isMirror(root->left, root->right);
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(2);
root->left->left = new TreeNode(3);
root->left->right = new TreeNode(4);
root->right->left = new TreeNode(4);
root->right->right = new TreeNode(3);
bool result = isSymmetric(root);
std::cout << (result ? "true" : "false") << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Check if the left subtree is a mirror of the right subtree.
✗ Incorrect
The given tree is symmetric because the left subtree is a mirror reflection of the right subtree.
The function isMirror recursively checks this property.
❓ Predict Output
intermediate2:00remaining
Output when trees are not symmetric
What is the output of the following C++ code that checks if two binary trees are symmetric?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
bool isSymmetric(TreeNode* root) {
if (!root) return true;
return isMirror(root->left, root->right);
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(2);
root->left->right = new TreeNode(3);
root->right->right = new TreeNode(3);
bool result = isSymmetric(root);
std::cout << (result ? "true" : "false") << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Check if the left subtree is a mirror of the right subtree.
✗ Incorrect
The tree is not symmetric because the left and right subtrees do not mirror each other.
🔧 Debug
advanced2:00remaining
Identify the error in symmetric tree check
What error does the following C++ code produce when checking if two trees are symmetric?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
bool isSymmetric(TreeNode* root) {
if (!root) return true;
return isMirror(root->left, root->right);
}
int main() {
TreeNode* root = nullptr;
bool result = isSymmetric(root);
std::cout << (result ? "true" : "false") << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Check if the root is null before accessing its children.
✗ Incorrect
The function isSymmetric does not check if root is null before accessing root->left and root->right.
This causes a segmentation fault at runtime.
🧠 Conceptual
advanced1:30remaining
Understanding symmetry in binary trees
Which of the following statements about symmetric binary trees is TRUE?
Attempts:
2 left
💡 Hint
Symmetry means mirror image, not identical structure.
✗ Incorrect
Symmetry in trees means the left subtree is a mirror reflection of the right subtree, not necessarily identical.
❓ Predict Output
expert2:30remaining
Output of complex symmetric tree check with null nodes
What is the output of the following C++ code that checks if two binary trees are symmetric?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool isMirror(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
bool isSymmetric(TreeNode* root) {
if (!root) return true;
return isMirror(root->left, root->right);
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(2);
root->left->left = nullptr;
root->left->right = new TreeNode(3);
root->right->left = nullptr;
root->right->right = new TreeNode(3);
bool result = isSymmetric(root);
std::cout << (result ? "true" : "false") << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Check if the left subtree is a mirror of the right subtree including null nodes.
✗ Incorrect
The tree is not symmetric because the left subtree's left child is null but the right subtree's left child is also null, but the right subtree's right child is not null, so the mirror condition fails.