Challenge - 5 Problems
Maximum Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Maximum Path Sum Calculation
What is the maximum path sum of the following binary tree?
DSA C
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
// Tree structure:
// 1
// / \
// 2 3
int maxPathSumHelper(struct TreeNode* root, int* maxSum) {
if (!root) return 0;
int left = maxPathSumHelper(root->left, maxSum);
int right = maxPathSumHelper(root->right, maxSum);
int max_single = (left > right ? left : right) + root->val;
if (max_single < root->val) max_single = root->val;
int max_top = left + right + root->val;
if (*maxSum < max_top) *maxSum = max_top;
if (*maxSum < max_single) *maxSum = max_single;
return max_single;
}
int maxPathSum(struct TreeNode* root) {
int maxSum = root->val;
maxPathSumHelper(root, &maxSum);
return maxSum;
}
// Construct tree nodes
struct TreeNode n1 = {1, NULL, NULL};
struct TreeNode n2 = {2, NULL, NULL};
struct TreeNode n3 = {3, NULL, NULL};
// Link nodes
n1.left = &n2;
n1.right = &n3;
int result = maxPathSum(&n1);
printf("%d\n", result);Attempts:
2 left
💡 Hint
Consider paths that can go through the root and both children.
✗ Incorrect
The maximum path sum is 2 + 1 + 3 = 6. The path includes left child, root, and right child.
🧠 Conceptual
intermediateUnderstanding Path Sum Constraints
Which statement about the maximum path sum in a binary tree is TRUE?
Attempts:
2 left
💡 Hint
Think about whether the path is restricted to root or leaves.
✗ Incorrect
The maximum path sum can be any path connecting nodes in the tree, not necessarily starting at root or ending at leaves.
🔧 Debug
advancedIdentify the Bug in Maximum Path Sum Code
What error will this code produce when calculating maximum path sum?
DSA C
int maxPathSumHelper(struct TreeNode* root, int* maxSum) { if (!root) return 0; int left = maxPathSumHelper(root->left, maxSum); int right = maxPathSumHelper(root->right, maxSum); int max_single = (left > right ? left : right) + root->val; int max_top = left + right + root->val; if (*maxSum < max_top) *maxSum = max_top; if (*maxSum < max_single) *maxSum = max_single; return max_single; } int maxPathSum(struct TreeNode* root) { int maxSum = 0; maxPathSumHelper(root, &maxSum); return maxSum; }
Attempts:
2 left
💡 Hint
Check initial maxSum value and how negative values are handled.
✗ Incorrect
Initializing maxSum to 0 causes incorrect results if all node values are negative because maxSum never updates to negative values.
📝 Syntax
advancedIdentify Syntax Error in Recursive Function
Which option contains a syntax error in the maxPathSumHelper function?
DSA C
int maxPathSumHelper(struct TreeNode* root, int* maxSum) { if (!root) return 0 int left = maxPathSumHelper(root->left, maxSum); int right = maxPathSumHelper(root->right, maxSum); int max_single = (left > right ? left : right) + root->val; int max_top = left + right + root->val; if (*maxSum < max_top) *maxSum = max_top; if (*maxSum < max_single) *maxSum = max_single; return max_single; }
Attempts:
2 left
💡 Hint
Look carefully at the line with 'return 0'.
✗ Incorrect
The line 'if (!root) return 0' is missing a semicolon at the end, causing a syntax error.
🚀 Application
expertMaximum Path Sum with Negative Values
Given the binary tree below, what is the maximum path sum?
// Tree structure:
// -10
// / \
// 9 20
// / \
// 15 7
DSA C
struct TreeNode n1 = {-10, NULL, NULL};
struct TreeNode n2 = {9, NULL, NULL};
struct TreeNode n3 = {20, NULL, NULL};
struct TreeNode n4 = {15, NULL, NULL};
struct TreeNode n5 = {7, NULL, NULL};
n1.left = &n2;
n1.right = &n3;
n3.left = &n4;
n3.right = &n5;
int result = maxPathSum(&n1);
printf("%d\n", result);Attempts:
2 left
💡 Hint
Consider the path 15 -> 20 -> 7 and also paths including root.
✗ Incorrect
The maximum path sum is 15 + 20 + 7 = 42. The path does not include the root -10 because it reduces the sum.
