Challenge - 5 Problems
Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Path Sum Check for Simple Tree
What is the output of the following C code that checks if there is a root-to-leaf path with the given sum in a binary tree?
DSA C
#include <stdio.h> #include <stdlib.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; int hasPathSum(struct TreeNode* root, int sum) { if (!root) return 0; if (!root->left && !root->right) return (sum == root->val); return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); } int main() { struct TreeNode n3 = {3, NULL, NULL}; struct TreeNode n4 = {4, NULL, NULL}; struct TreeNode n5 = {5, NULL, NULL}; struct TreeNode n2 = {2, &n4, &n5}; struct TreeNode root = {1, &n3, &n2}; int result = hasPathSum(&root, 8); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Trace the path sums from root to each leaf node and check if any equals 8.
✗ Incorrect
The paths are 1->3 (sum 4), 1->2->4 (sum 7), and 1->2->5 (sum 8). Since 1->2->5 sums to 8, the function returns 1 (true).
🧠 Conceptual
intermediateNumber of Paths with Given Sum in Binary Tree
If a binary tree has 3 root-to-leaf paths with sums 7, 8, and 10 respectively, how many paths will the function hasPathSum(root, 8) return true for?
Attempts:
2 left
💡 Hint
The function returns true if at least one path matches the sum.
✗ Incorrect
Only one path has sum 8, so the function returns true (1).
🔧 Debug
advancedIdentify the Bug in Path Sum Function
What error will this code produce when checking path sum in a binary tree?
DSA C
int hasPathSum(struct TreeNode* root, int sum) { if (!root) return 0; if (!root->left && !root->right) return (sum == root->val); return hasPathSum(root->left, sum - root->val) && hasPathSum(root->right, sum - root->val); }
Attempts:
2 left
💡 Hint
Check the logical operator used to combine recursive calls.
✗ Incorrect
Using && requires both left and right subtrees to have matching paths, which fails if one child is NULL.
❓ Predict Output
advancedOutput of Path Sum Check with Negative Values
What is the output of this code checking path sum in a tree with negative values?
DSA C
#include <stdio.h> #include <stdlib.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; int hasPathSum(struct TreeNode* root, int sum) { if (!root) return 0; if (!root->left && !root->right) return (sum == root->val); return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); } int main() { struct TreeNode n1 = {1, NULL, NULL}; struct TreeNode n_2 = {-2, NULL, NULL}; struct TreeNode n_3 = {-3, NULL, NULL}; struct TreeNode n11 = {11, NULL, NULL}; struct TreeNode n_5 = {-5, &n11, NULL}; struct TreeNode root = {10, &n_2, &n_3}; n_2.left = &n1; n_2.right = &n_5; int result = hasPathSum(&root, 18); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Check all root-to-leaf paths and their sums carefully.
✗ Incorrect
The paths and sums are: 10->-2->1=9, 10->-2->-5->11=14, 10->-3=7. None equals 18, so output is 0.
🚀 Application
expertCount All Root-to-Leaf Paths with Given Sum
Given a binary tree, which code snippet correctly counts the number of root-to-leaf paths that sum to a target value?
Attempts:
2 left
💡 Hint
Count paths by adding counts from left and right subtrees; return 1 only if leaf matches sum.
✗ Incorrect
Option A correctly returns 1 for matching leaf paths and sums counts from subtrees. Others use wrong return values or operators.
