0
0
DSA Javascriptprogramming~10 mins

Path Sum Root to Leaf in Binary Tree in DSA Javascript - 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 Javascript
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 '!=' or '!==' which changes the logic.
Using '>' or '<' which are not suitable for null checks.
2fill in blank
medium

Complete the code to check if the current node is a leaf node.

DSA Javascript
if (node.left === null && node.[1] === null) {
Drag options to blanks, or click blank then click option'
Aright
Bvalue
Cparent
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'parent' or 'value' instead of 'right'.
Using 'root' which is not a property of the node.
3fill in blank
hard

Fix the error in the recursive call to check left subtree path sum.

DSA Javascript
return hasPathSum(node.left, sum [1] node.value) || hasPathSum(node.right, sum - node.value);
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' which leads to wrong sum calculation.
Using '*' or '/' which are not relevant here.
4fill in blank
hard

Fill both blanks to complete the base case for leaf node sum check.

DSA Javascript
if (node.left === null && node.right === null) {
  return sum [1] node.value;
}
Drag options to blanks, or click blank then click option'
A>
B===
C!==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' or '!==', which would invert the logic.
Using '>' or '<' which are not correct for equality check.
5fill in blank
hard

Fill all three blanks to complete the recursive function for path sum.

DSA Javascript
function hasPathSum(node, sum) {
  if (node [1] null) return false;
  if (node.left === null && node.right === null) {
    return sum [2] node.value;
  }
  return hasPathSum(node.left, sum [3] node.value) || hasPathSum(node.right, sum - node.value);
}
Drag options to blanks, or click blank then click option'
A===
B-
C!==
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '===' to check node null which reverses logic.
Using '+' instead of '-' in recursive calls.
Using '!=' instead of '===' for sum comparison.