0
0
DSA C++programming~10 mins

Check if Two Trees are Symmetric in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A==
B&&
C!=
D||
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.
2fill in blank
medium

Complete 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'
A&&
B==
C!=
D||
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.
3fill in blank
hard

Fix 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'
A==
B>
C!=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using == returns false when values are equal, which is wrong.
Using < or > is not correct for equality check.
4fill in blank
hard

Fill 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'
Aleft
Bright
Cval
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right incorrectly.
Using 'val' or 'parent' instead of child pointers.
5fill in blank
hard

Fill 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'
Aleft
Bright
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Passing root itself instead of children.
Using != instead of == in null check.