0
0
DSA Typescriptprogramming~10 mins

Check if Two Trees are Symmetric in DSA Typescript - 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 both nodes are null, which means symmetry at this point.

DSA Typescript
if (root1 === null && root2 [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 symmetry check.
Using '>' or '<' operators here is incorrect.
2fill in blank
medium

Complete the code to check if either node is null, which means trees are not symmetric.

DSA Typescript
if (root1 === null || root2 [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 symmetry detection.
Using '>' or '<' operators here is incorrect.
3fill in blank
hard

Fix the error in comparing the values of the two nodes to check symmetry.

DSA Typescript
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 '==' instead of '!=' causes wrong symmetry detection.
Using '>' or '<' operators here is incorrect.
4fill in blank
hard

Fill both blanks to recursively check the left subtree of root1 with the right subtree of root2 and vice versa.

DSA Typescript
return isSymmetricHelper(root1.[1], root2.[2]) && isSymmetricHelper(root1.right, root2.left);
Drag options to blanks, or click blank then click option'
Aleft
Bright
Croot
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right incorrectly.
Using 'val' or 'root' instead of child nodes.
5fill in blank
hard

Fill all three blanks to define the helper function signature and call it from the main function.

DSA Typescript
function [1](root1: TreeNode | null, root2: TreeNode | null): boolean {
  // helper logic here
}

function [2](root: TreeNode | null): boolean {
  if (root === null) return true;
  return [3](root.left, root.right);
}
Drag options to blanks, or click blank then click option'
AisSymmetricHelper
BisSymmetric
DcheckSymmetry
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up function names.
Not calling the helper function from main.