Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the BST node is null.
DSA Java
if (root [1] null) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' causes assignment, not comparison.
Using '!=' reverses the logic.
✗ Incorrect
We check if root is equal to null using '=='.
2fill in blank
mediumComplete the code to add a value to the HashSet.
DSA Java
set.[1](root.val);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contains' instead of 'add' does not insert the value.
Using 'remove' deletes elements, which is incorrect here.
✗ Incorrect
We add the current node's value to the set using 'add'.
3fill in blank
hardFix the error in the condition to check if the complement exists in the set.
DSA Java
if (set.[1](k - root.val)) { return true; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' instead of 'contains' changes the set and returns a boolean about insertion.
Using 'remove' deletes elements, which is not intended here.
✗ Incorrect
We use 'contains' to check if the complement is in the set.
4fill in blank
hardFill both blanks to complete the recursive calls for left and right subtrees.
DSA Java
return dfs(root.[1], set, k) || dfs(root.[2], set, k);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'parent' instead of 'left' or 'right' causes errors.
Swapping left and right does not cause errors but is less clear.
✗ Incorrect
We recursively call dfs on the left and right children of the current node.
5fill in blank
hardFill all three blanks to complete the Two Sum in BST method using DFS and a HashSet.
DSA Java
public boolean findTarget(TreeNode root, int k) {
Set<Integer> set = new HashSet<>();
return dfs(root, [1], k);
}
private boolean dfs(TreeNode root, Set<Integer> [2], int k) {
if (root == null) return false;
if ([3].contains(k - root.val)) return true;
[2].add(root.val);
return dfs(root.left, [2], k) || dfs(root.right, [2], k);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes compilation errors.
Using undefined variables causes runtime errors.
✗ Incorrect
The HashSet named 'set' is passed and used to track visited values.
