Challenge - 5 Problems
Symmetry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int isMirror(Node* t1, Node* t2) {
if (t1 == NULL && t2 == NULL) return 1;
if (t1 == NULL || t2 == NULL) return 0;
return (t1->data == t2->data) && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
}
int main() {
Node n1 = {1, NULL, NULL};
Node n2 = {2, NULL, NULL};
Node n3 = {2, NULL, NULL};
Node n4 = {1, NULL, NULL};
n1.left = &n2;
n1.right = &n3;
n2.left = NULL;
n2.right = NULL;
n3.left = NULL;
n3.right = NULL;
n4.left = &n3;
n4.right = &n2;
int result = isMirror(&n1, &n4);
printf("%d\n", result);
return 0;
}Attempts:
2 left
💡 Hint
Think about how the function compares nodes and their mirrored children.
✗ Incorrect
The function isMirror returns 1 if the two trees are mirror images. Here, the trees are symmetric, so output is 1.
❓ Predict Output
intermediateOutput when One Tree is NULL
What will be the output of the following code snippet?
DSA C
Node* t1 = NULL;
Node n2 = {5, NULL, NULL};
int result = isMirror(t1, &n2);
printf("%d\n", result);Attempts:
2 left
💡 Hint
Check the base cases in isMirror function.
✗ Incorrect
If one tree is NULL and the other is not, they are not mirrors, so output is 0.
🔧 Debug
advancedIdentify the Bug in Symmetry Check Function
Find the bug in this C function that checks if two trees are symmetric.
DSA C
int isMirror(Node* t1, Node* t2) { if (t1 == NULL && t2 == NULL) return 1; if (t1 == NULL || t2 == NULL) return 0; if (t1->data != t2->data) return 0; return isMirror(t1->left, t2->left) && isMirror(t1->right, t2->right); }
Attempts:
2 left
💡 Hint
Think about what it means for two trees to be mirror images.
✗ Incorrect
To check mirror symmetry, left subtree of one must match right subtree of the other and vice versa.
🧠 Conceptual
advancedUnderstanding Symmetry in Binary Trees
Which statement best describes when two binary trees are symmetric?
Attempts:
2 left
💡 Hint
Symmetry means mirror image, not just identical.
✗ Incorrect
Symmetric trees are mirror images with matching node values and structure when flipped horizontally.
🚀 Application
expertResult of Symmetry Check on Complex Trees
Given the following two trees, what is the output of isMirror function?
DSA C
Node n1 = {1, NULL, NULL};
Node n2 = {2, NULL, NULL};
Node n3 = {2, NULL, NULL};
Node n4 = {3, NULL, NULL};
Node n5 = {3, NULL, NULL};
n1.left = &n2;
n1.right = &n4;
n2.left = NULL;
n2.right = &n3;
n3.left = NULL;
n3.right = NULL;
n4.left = &n5;
n4.right = NULL;
n5.left = NULL;
n5.right = NULL;
Node m1 = {1, NULL, NULL};
Node m2 = {3, NULL, NULL};
Node m3 = {2, NULL, NULL};
Node m4 = {2, NULL, NULL};
Node m5 = {3, NULL, NULL};
m1.left = &m2;
m1.right = &m4;
m2.left = NULL;
m2.right = NULL;
m4.left = &m5;
m4.right = NULL;
m5.left = NULL;
m5.right = &m3;
m3.left = NULL;
m3.right = NULL;
int result = isMirror(&n1, &m1);
printf("%d\n", result);Attempts:
2 left
💡 Hint
Trace the mirrored structure carefully comparing left and right subtrees.
✗ Incorrect
The trees differ in structure and node arrangement, so they are not mirrors; output is 0.
