Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the root nodes of two trees are both null.
DSA Java
if (root1 == null && root2 [1] null) { return true; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes wrong logic.
Using comparison operators like '>' or '<' on objects.
✗ Incorrect
We check if both root1 and root2 are null using '=='. If both are null, trees are symmetric at this node.
2fill in blank
mediumComplete the code to check if one of the nodes is null while the other is not.
DSA Java
if (root1 == null || root2 [1] null) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes incorrect checks.
Using logical AND instead of OR.
✗ Incorrect
If one node is null and the other is not, trees are not symmetric, so return false.
3fill in blank
hardFix the error in comparing the values of the two nodes.
DSA Java
if (root1.val [1] root2.val) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' reverses the logic.
Using comparison operators like '>' or '<' instead of equality.
✗ Incorrect
If the values of the two nodes are not equal, the trees are not symmetric.
4fill in blank
hardFill both blanks to recursively check symmetry of left and right subtrees.
DSA Java
return isSymmetric(root1.[1], root2.[2]) && isSymmetric(root1.[2], root2.[1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same child (left or right) for both calls.
Using node values instead of child nodes.
✗ Incorrect
We compare root1's left with root2's right and root1's right with root2's left to check mirror symmetry.
5fill in blank
hardFill all three blanks to complete the helper method signature and initial call.
DSA Java
public boolean isSymmetric(TreeNode [1], TreeNode [2]) { if ([1] == null && [2] == null) return true; if ([1] == null || [2] == null) return false; if ([1].val != [2].val) return false; return isSymmetric([1].left, [2].right) && isSymmetric([1].right, [2].left); }
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing variable names between parameters and inside the method.
Using root1 and root2 inconsistently.
✗ Incorrect
The helper method uses node1 and node2 as parameters and variables to compare nodes recursively.
