0
0
DSA Typescriptprogramming~10 mins

Path Sum Root to Leaf in Binary Tree 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 the current node is null.

DSA Typescript
if (node [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 logic.
Using '>' or '<' operators for null check.
2fill in blank
medium

Complete 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'
A<
B!=
C>
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '===' causes incorrect leaf detection.
Checking only one child for null.
3fill in blank
hard

Fix 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'
A/
B-
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' causes decreasing sum incorrectly.
Using '*' or '/' causes wrong calculations.
4fill in blank
hard

Fill 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'
Anode
Broot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node' in parameter but calling helper with 'root'.
Mismatch between parameter name and argument.
5fill in blank
hard

Fill 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'
Afalse
BtargetSum
C+
Dtrue
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.