Challenge - 5 Problems
Maximum Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Maximum Path Sum Calculation
What is the output of the following C++ code that calculates the maximum path sum in a binary tree?
DSA C++
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
int maxPathSumUtil(TreeNode* root, int& maxSum) {
if (!root) return 0;
int left = std::max(0, maxPathSumUtil(root->left, maxSum));
int right = std::max(0, maxPathSumUtil(root->right, maxSum));
maxSum = std::max(maxSum, left + right + root->val);
return root->val + std::max(left, right);
}
int maxPathSum(TreeNode* root) {
int maxSum = INT_MIN;
maxPathSumUtil(root, maxSum);
return maxSum;
}
int main() {
TreeNode* root = new TreeNode(-10);
root->left = new TreeNode(9);
root->right = new TreeNode(20);
root->right->left = new TreeNode(15);
root->right->right = new TreeNode(7);
std::cout << maxPathSum(root);
return 0;
}Attempts:
2 left
💡 Hint
Consider the path that includes nodes 15, 20, and 7.
✗ Incorrect
The maximum path sum is 42, which comes from the path 15 -> 20 -> 7. The root node (-10) and left child (9) do not contribute to a higher sum.
🧠 Conceptual
intermediate1:30remaining
Understanding Path Sum Components
In the maximum path sum problem, why do we use std::max(0, left) and std::max(0, right) when calculating the maximum gain from left and right subtrees?
Attempts:
2 left
💡 Hint
Think about how negative values affect the sum.
✗ Incorrect
We use std::max(0, left) and std::max(0, right) to ignore any negative sums from subtrees because including a negative sum would decrease the overall path sum. This way, we only add positive contributions or zero.
🔧 Debug
advanced2:00remaining
Identify the Bug in Maximum Path Sum Code
What error will the following code produce when run, and why?
DSA C++
int maxPathSumUtil(TreeNode* root, int& maxSum) { if (root == nullptr) return 0; int left = maxPathSumUtil(root->left, maxSum); int right = maxPathSumUtil(root->right, maxSum); maxSum = std::max(maxSum, left + right + root->val); return root->val + std::max(left, right); } int maxPathSum(TreeNode* root) { int maxSum = 0; maxPathSumUtil(root, maxSum); return maxSum; }
Attempts:
2 left
💡 Hint
Check the initial value of maxSum and how negative values are handled.
✗ Incorrect
The initial maxSum is set to 0, so if all node values are negative, maxSum will never update to a negative value, resulting in an incorrect maximum path sum.
🚀 Application
advanced1:30remaining
Maximum Path Sum in a Tree with Negative and Positive Values
Given the binary tree below, what is the maximum path sum?
2
/ \
-1 3
/ \
-2 4
Attempts:
2 left
💡 Hint
Consider the path that includes nodes 2, 3, and 4.
✗ Incorrect
The maximum path sum is 9, coming from the path 2 -> 3 -> 4. Other paths like 3 -> 4 sum to 7, which is lower.
❓ Predict Output
expert2:00remaining
Output of Maximum Path Sum with Single Node Negative Tree
What is the output of the following code when the tree has a single node with value -5?
DSA C++
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
int maxPathSumUtil(TreeNode* root, int& maxSum) {
if (!root) return 0;
int left = std::max(0, maxPathSumUtil(root->left, maxSum));
int right = std::max(0, maxPathSumUtil(root->right, maxSum));
maxSum = std::max(maxSum, left + right + root->val);
return root->val + std::max(left, right);
}
int maxPathSum(TreeNode* root) {
int maxSum = INT_MIN;
maxPathSumUtil(root, maxSum);
return maxSum;
}
int main() {
TreeNode* root = new TreeNode(-5);
std::cout << maxPathSum(root);
return 0;
}Attempts:
2 left
💡 Hint
The maximum path sum can be a single node value if all others are negative or absent.
✗ Incorrect
Since the tree has only one node with value -5, the maximum path sum is -5.