Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if two nodes are both null.
DSA C++
if (root1 == nullptr && root2 [1] nullptr) return true;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using && causes a syntax error (root2 && nullptr).
Using != checks if root2 is not null, which is incorrect.
Using || changes the logic to OR, which is wrong.
✗ Incorrect
We check if both root1 and root2 are null by using == to compare root2 with nullptr.
2fill in blank
mediumComplete the code to check if one node is null and the other is not.
DSA C++
if (root1 == nullptr || root2 [1] nullptr) return false;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using != fails to detect root1 non-null and root2 null.
Using && requires both null, already handled.
Using || is misplaced; it's already between conditions.
✗ Incorrect
Since the both-null case is handled earlier, we check if root1 == nullptr || root2 == nullptr to catch exactly one null.
3fill in blank
hardFix the error in comparing node values for symmetry.
DSA C++
if (root1->val [1] root2->val) return false;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == returns false when values are equal, which is wrong.
Using < or > is not correct for equality check.
✗ Incorrect
We return false if the values are not equal, so we use !=.
4fill in blank
hardFill both blanks to recursively check left and right subtrees for symmetry.
DSA C++
return isSymmetricHelper(root1->[1], root2->[2]) && isSymmetricHelper(root1->[2], root2->[1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right incorrectly.
Using 'val' or 'parent' instead of child pointers.
✗ 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 calling the helper.
DSA C++
bool isSymmetric(TreeNode* root) {
if (root == nullptr) return true;
return isSymmetricHelper(root->[1], root->[2]);
}
bool isSymmetricHelper(TreeNode* root1, TreeNode* root2) {
if (root1 == nullptr && root2 == nullptr) return true;
if (root1 == nullptr || root2 [3] nullptr) return false;
if (root1->val != root2->val) return false;
return isSymmetricHelper(root1->left, root2->right) && isSymmetricHelper(root1->right, root2->left);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing root itself instead of children.
Using != instead of == in null check.
✗ Incorrect
The main function calls helper with root's left and right children. The helper checks if one node is null and the other is not using ==.