0
0
DSA Javascriptprogramming~10 mins

Check if Two Trees are Symmetric in DSA Javascript - 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 Javascript
if (node1 === null && node2 [1] null) return true;
Drag options to blanks, or click blank then click option'
A&&
B!==
C===
D||
Attempts:
3 left
💡 Hint
Common Mistakes
Using || instead of && causes wrong logic.
Using !== between node2 and null instead of ===.
2fill in blank
medium

Complete the code to check if one node is null and the other is not.

DSA Javascript
if (node1 === null || node2 [1] null) return false;
Drag options to blanks, or click blank then click option'
A===
B!==
C&&
D||
Attempts:
3 left
💡 Hint
Common Mistakes
Using === instead of !== causes wrong detection.
Using logical operators instead of comparison operators.
3fill in blank
hard

Fix the error in comparing node values for symmetry.

DSA Javascript
if (node1.val [1] node2.val) return false;
Drag options to blanks, or click blank then click option'
A!==
B===
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using === causes wrong logic because it returns false when values differ.
Using < or > is incorrect for equality check.
4fill in blank
hard

Fill both blanks to recursively check left and right subtrees for symmetry.

DSA Javascript
return isSymmetricHelper(node1.[1], node2.[2]) && isSymmetricHelper(node1.[2], node2.[1]);
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cval
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'parent' instead of 'left' or 'right' causes errors.
Swapping left and right incorrectly breaks symmetry check.
5fill in blank
hard

Fill all three blanks to define the main function calling the helper.

DSA Javascript
function isSymmetric(root) {
  if (root === null) return true;
  return isSymmetricHelper(root.[1], root.[2]);
}

function isSymmetricHelper(node1, node2) {
  if (node1 === null && node2 === null) return true;
  if (node1 === null || node2 === null) return false;
  if (node1.val [3] node2.val) return false;
  return isSymmetricHelper(node1.left, node2.right) && isSymmetricHelper(node1.right, node2.left);
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
C!==
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Using === instead of !== for value comparison.
Swapping left and right in the main function call.