Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current node is null.
DSA Typescript
if (node [1] null) return false;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '===' causes wrong logic.
Using '>' or '<' operators for null check.
✗ Incorrect
We check if the node is exactly equal to null to stop recursion.
2fill in blank
mediumComplete the code to check if the current node is a leaf node.
DSA Typescript
if (node.left === null && node.right [1] null) return sum + node.val === targetSum;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '===' causes incorrect leaf detection.
Checking only one child for null.
✗ Incorrect
A leaf node has both left and right children as null.
3fill in blank
hardFix the error in the recursive call to check left subtree.
DSA Typescript
return helper(node.left, sum [1] node.val, targetSum) || helper(node.right, sum + node.val, targetSum);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' causes decreasing sum incorrectly.
Using '*' or '/' causes wrong calculations.
✗ Incorrect
We add the current node's value to the sum when going down the tree.
4fill in blank
hardFill both blanks to complete the function signature and initial call.
DSA Typescript
function hasPathSum([1]: TreeNode | null, targetSum: number): boolean { return helper([2], 0, targetSum); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node' in parameter but calling helper with 'root'.
Mismatch between parameter name and argument.
✗ Incorrect
The function parameter is named 'root' and the helper is called with 'root'.
5fill in blank
hardFill all three blanks to complete the helper function for path sum check.
DSA Typescript
function helper(node: TreeNode | null, sum: number, targetSum: number): boolean {
if (node === null) return [1];
if (node.left === null && node.right === null) return sum + node.val === [2];
return helper(node.left, sum [3] node.val, targetSum) || helper(node.right, sum + node.val, targetSum);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning true when node is null causes wrong results.
Comparing sum instead of sum + node.val to targetSum.
Using wrong operator instead of '+' in recursion.
✗ Incorrect
Return false if node is null, compare sum + node.val to targetSum, and add node.val to sum in recursion.