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 given tree and sum
What is the output of the following C++ code that checks if there is a root-to-leaf path with sum 22?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool hasPathSum(TreeNode* root, int sum) {
if (!root) return false;
if (!root->left && !root->right) return root->val == sum;
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
int main() {
TreeNode* root = new TreeNode(5);
root->left = new TreeNode(4);
root->right = new TreeNode(8);
root->left->left = new TreeNode(11);
root->left->left->left = new TreeNode(7);
root->left->left->right = new TreeNode(2);
root->right->left = new TreeNode(13);
root->right->right = new TreeNode(4);
root->right->right->right = new TreeNode(1);
bool result = hasPathSum(root, 22);
std::cout << (result ? "true" : "false") << std::endl;
return 0;
}Attempts:
2 left
š” Hint
Trace the path sums from root to leaf nodes carefully.
ā Incorrect
The path 5->4->11->2 sums to 22, so the function returns true.
š§ Conceptual
intermediateNumber of root-to-leaf paths with given sum
Given a binary tree and a sum, how many root-to-leaf paths have the exact sum? Consider the tree below and sum = 26.
Tree structure:
5
āā4
ā āā11
ā āā7
ā āā2
āā8
āā13
āā4
āā1
Attempts:
2 left
š” Hint
Check all root-to-leaf paths and sum their values.
ā Incorrect
Paths 5->4->11->7 and 5->8->13 sum to 27 and 26 respectively, only one path sums to 26, so answer is 1.
š§ Debug
advancedIdentify the error in path sum function
What error does the following code produce when checking for path sum in a binary tree?
DSA C++
bool hasPathSum(TreeNode* root, int sum) { if (!root) return false; if (!root->left && !root->right) return root->val = sum; return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); }
Attempts:
2 left
š” Hint
Check the use of '=' vs '==' in the return statement.
ā Incorrect
The code uses '=' (assignment) instead of '==' (comparison) in the return statement, causing a syntax or logical error.
š Application
advancedFind all root-to-leaf paths with given sum
Which option correctly returns all root-to-leaf paths where the sum of node values equals the target sum?
DSA C++
void findPaths(TreeNode* root, int sum, std::vector<int>& path, std::vector<std::vector<int>>& result) { if (!root) return; path.push_back(root->val); if (!root->left && !root->right && root->val == sum) { result.push_back(path); } else { findPaths(root->left, sum - root->val, path, result); findPaths(root->right, sum - root->val, path, result); } path.pop_back(); }
Attempts:
2 left
š” Hint
Check if path is popped after recursion to backtrack.
ā Incorrect
The function pushes current node, checks leaf and sum, recurses, then pops node to backtrack correctly.
ā Predict Output
expertOutput of path sum check with negative values
What is the output of the following code snippet that checks for a path sum of 0 in a tree with negative values?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool hasPathSum(TreeNode* root, int sum) {
if (!root) return false;
if (!root->left && !root->right) return root->val == sum;
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
int main() {
TreeNode* root = new TreeNode(-2);
root->right = new TreeNode(-3);
bool result = hasPathSum(root, 0);
std::cout << (result ? "true" : "false") << std::endl;
return 0;
}Attempts:
2 left
š” Hint
Check if any root-to-leaf path sums to zero.
ā Incorrect
The paths are -2->-3 = -5 and -2 alone is not a leaf path, so no path sums to 0.
