Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current node is a leaf node.
DSA Java
if (root.left == null && root.right [1] null) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes wrong leaf detection.
Using comparison operators like '>' or '<' is invalid for null checks.
✗ Incorrect
We check if both left and right children are null to identify a leaf node.
2fill in blank
mediumComplete the code to subtract the current node's value from the target sum.
DSA Java
int newSum = targetSum [1] root.val;Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds instead of subtracting, causing incorrect sum calculation.
Using '*' or '/' is not meaningful in this context.
✗ Incorrect
We subtract the current node's value to update the remaining sum needed.
3fill in blank
hardFix the error in the recursive call to check left subtree.
DSA Java
return hasPathSum(root.left, [1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Passing targetSum unchanged causes infinite recursion or wrong results.
Adding root.val instead of subtracting reverses logic.
✗ Incorrect
We must pass the updated sum after subtracting current node's value.
4fill in blank
hardFill both blanks to check if either left or right subtree has a valid path.
DSA Java
return hasPathSum(root.[1], [2]) || hasPathSum(root.right, targetSum - root.val);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' for the first blank causes checking right child twice.
Using 'targetSum + root.val' increases sum incorrectly.
✗ Incorrect
We check left subtree with updated sum and right subtree with updated sum.
5fill in blank
hardFill all three blanks to complete the method signature and base case check.
DSA Java
public boolean hasPathSum([1] root, int [2]) { if (root == [3]) return false;
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names causes compilation errors.
Checking root against wrong value instead of null.
✗ Incorrect
Method takes TreeNode root and int targetSum; base case returns false if root is null.
