Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' or '!==' which changes the logic.
Using '>' or '<' which are not suitable for null checks.
✗ Incorrect
We check if the node is exactly equal to null to know if we reached a leaf's child.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'parent' or 'value' instead of 'right'.
Using 'root' which is not a property of the node.
✗ Incorrect
A leaf node has no left or right children, so both must be null.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' which leads to wrong sum calculation.
Using '*' or '/' which are not relevant here.
✗ Incorrect
We subtract the current node's value from sum to check the remaining sum in the subtree.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' or '!==', which would invert the logic.
Using '>' or '<' which are not correct for equality check.
✗ Incorrect
At a leaf node, check if the remaining sum equals the node's value.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Check node not null with '!==', compare sum and node.value with '===', and subtract node.value from sum in recursion.