Challenge - 5 Problems
Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of the path sum check?
Given the binary tree and target sum, what is the output of the function call?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function hasPathSum(root, targetSum) { if (!root) return false; if (!root.left && !root.right) return root.val === targetSum; return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); } const tree = new TreeNode(5, new TreeNode(4, new TreeNode(11, new TreeNode(7), new TreeNode(2) ) ), new TreeNode(8, new TreeNode(13), new TreeNode(4, null, new TreeNode(1) ) ) ); console.log(hasPathSum(tree, 22));
Attempts:
2 left
💡 Hint
Check if any root-to-leaf path sums exactly to 22.
✗ Incorrect
The path 5 -> 4 -> 11 -> 2 sums to 22, so the function returns true.
❓ Predict Output
intermediate2:00remaining
What is the output when no path matches the sum?
What does the function return for the given tree and target sum 26?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function hasPathSum(root, targetSum) { if (!root) return false; if (!root.left && !root.right) return root.val === targetSum; return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); } const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3) ); console.log(hasPathSum(tree, 26));
Attempts:
2 left
💡 Hint
No root-to-leaf path sums to 26 here.
✗ Incorrect
Paths are 1->2=3 and 1->3=4, none equals 26, so returns false.
🔧 Debug
advanced2:00remaining
What error does this code raise?
Identify the error when running this code snippet.
DSA Javascript
function hasPathSum(root, targetSum) {
if (!root) return false
if (!root.left && !root.right) return root.val === targetSum
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val)
}
const tree = { val: 1, left: null, right: null };
console.log(hasPathSum(tree, 1));Attempts:
2 left
💡 Hint
Check for missing or mismatched parentheses.
✗ Incorrect
The last return statement misses a closing parenthesis, causing SyntaxError.
❓ Predict Output
advanced2:00remaining
What is the output of this iterative path sum check?
What does the function return for the given tree and target sum 18?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function hasPathSumIterative(root, targetSum) { if (!root) return false; const stack = [{ node: root, currentSum: root.val }]; while (stack.length) { const { node, currentSum } = stack.pop(); if (!node.left && !node.right && currentSum === targetSum) return true; if (node.right) stack.push({ node: node.right, currentSum: currentSum + node.right.val }); if (node.left) stack.push({ node: node.left, currentSum: currentSum + node.left.val }); } return false; } const tree = new TreeNode(10, new TreeNode(5, new TreeNode(3), new TreeNode(2) ), new TreeNode(7) ); console.log(hasPathSumIterative(tree, 18));
Attempts:
2 left
💡 Hint
Check if any root-to-leaf path sums to 18 using stack.
✗ Incorrect
The path 10 -> 5 -> 3 sums to 18, so returns true.
🧠 Conceptual
expert2:00remaining
How many root-to-leaf paths sum to 22?
Given the binary tree below, how many root-to-leaf paths have a sum equal to 22?
Tree structure:
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
Attempts:
2 left
💡 Hint
Trace all root-to-leaf paths and sum their values.
✗ Incorrect
There is one root-to-leaf path that sums to 22: 5 -> 4 -> 11 -> 2 (5+4+11+2=22). The other paths sum to 27 (5->4->11->7), 26 (5->8->13), and 18 (5->8->4->1).