Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if both trees are NULL, which means they are symmetric.
DSA C
if (root1 == NULL && root2 == [1]) { return 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking only one tree for NULL
Using 0 instead of NULL
✗ Incorrect
Both trees being NULL means they are symmetric, so we check if root2 is NULL.
2fill in blank
mediumComplete the code to check if one tree is NULL and the other is not, which means they are not symmetric.
DSA C
if (root1 == NULL || root2 == [1]) { return 0; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using AND instead of OR
Returning 1 instead of 0
✗ Incorrect
If one tree is NULL and the other is not, they cannot be symmetric.
3fill in blank
hardFix the error in the comparison of node values to check if they are equal.
DSA C
if (root1->val [1] root2->val) { return 0; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of !=
Using > or < which only checks order
✗ Incorrect
If the values are not equal, the trees are not symmetric, so we return 0.
4fill in blank
hardFill both blanks to recursively check if left subtree of root1 is symmetric with right subtree of root2 and vice versa.
DSA C
return isSymmetricHelper(root1->[1], root2->[2]) && isSymmetricHelper(root1->right, root2->left);
Attempts:
3 left
💡 Hint
Common Mistakes
Using left for both subtrees
Using right for both subtrees
✗ Incorrect
We compare root1's left with root2's right and root1's right with root2's left for symmetry.
5fill in blank
hardFill all three blanks to complete the main function that calls the helper to check if a tree is symmetric with itself.
DSA C
int isSymmetric(struct TreeNode* root) {
if (root == NULL) return 1;
return isSymmetricHelper(root, [1]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing root->left or root->right instead of root
Passing NULL as second argument
✗ Incorrect
We check if the tree is symmetric by comparing it with itself using the helper function.
