Challenge - 5 Problems
DP on Trees Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Maximum Path Sum in a Simple Binary Tree
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;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
int maxSum = INT_MIN;
int maxPathSumUtil(TreeNode* root) {
if (!root) return 0;
int left = max(0, maxPathSumUtil(root->left));
int right = max(0, maxPathSumUtil(root->right));
maxSum = max(maxSum, root->val + left + right);
return root->val + max(left, right);
}
int maxPathSum(TreeNode* root) {
maxSum = INT_MIN;
maxPathSumUtil(root);
return maxSum;
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
int result = maxPathSum(root);
std::cout << result << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Consider the path that includes both left and right children plus the root.
✗ Incorrect
The maximum path sum is 2 + 1 + 3 = 6. The code calculates the maximum sum of any path in the tree, including paths that pass through the root and both children.
❓ Predict Output
intermediateMaximum Path Sum with Negative Values
What is the output of the following C++ code when the tree contains negative values?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
int maxSum = INT_MIN;
int maxPathSumUtil(TreeNode* root) {
if (!root) return 0;
int left = max(0, maxPathSumUtil(root->left));
int right = max(0, maxPathSumUtil(root->right));
maxSum = max(maxSum, root->val + left + right);
return root->val + max(left, right);
}
int maxPathSum(TreeNode* root) {
maxSum = INT_MIN;
maxPathSumUtil(root);
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);
int result = maxPathSum(root);
std::cout << result << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
The maximum path includes nodes 15, 20, and 7.
✗ Incorrect
The maximum path sum is 15 + 20 + 7 = 42. The path does not include the root node -10 because it reduces the sum.
🔧 Debug
advancedIdentify the Runtime Error in Maximum Path Sum Code
The following code is intended to find the maximum path sum in a binary tree but causes a runtime error. What is the cause?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
int maxSum = INT_MIN;
int maxPathSumUtil(TreeNode* root) {
if (root == nullptr) return 0;
int left = maxPathSumUtil(root->left);
int right = maxPathSumUtil(root->right);
maxSum = max(maxSum, root->val + left + right);
return root->val + max(left, right);
}
int maxPathSum(TreeNode* root) {
maxSum = INT_MIN;
maxPathSumUtil(root);
return maxSum;
}
int main() {
TreeNode* root = new TreeNode(-3);
int result = maxPathSum(root);
std::cout << result << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Consider how negative values affect the path sums and the maxSum variable.
✗ Incorrect
The code does not ignore negative sums from left or right subtrees, so it may add negative values to maxSum, causing incorrect results. It should use max(0, ...) to ignore negative paths.
🧠 Conceptual
advancedUnderstanding the Role of max(0, ... ) in DP on Trees Maximum Path Sum
Why do we use max(0, maxPathSumUtil(child)) when calculating the maximum path sum in a binary tree?
Attempts:
2 left
💡 Hint
Think about what happens if a subtree has a negative sum.
✗ Incorrect
Using max(0, ...) ignores negative sums from child nodes, preventing them from decreasing the total path sum. This ensures the path sum only increases or stays the same.
🚀 Application
expertMaximum Path Sum in a Tree with Multiple Branches (N-ary Tree)
Given the following code snippet for an N-ary tree, what is the output of the maximum path sum calculation?
DSA C++
struct Node {
int val;
std::vector<Node*> children;
Node(int x) : val(x) {}
};
int maxSum = INT_MIN;
int maxPathSumUtil(Node* root) {
if (!root) return 0;
int max1 = 0, max2 = 0;
for (auto child : root->children) {
int val = maxPathSumUtil(child);
if (val > max1) {
max2 = max1;
max1 = val;
} else if (val > max2) {
max2 = val;
}
}
maxSum = std::max(maxSum, root->val + max1 + max2);
return root->val + max1;
}
int maxPathSum(Node* root) {
maxSum = INT_MIN;
maxPathSumUtil(root);
return maxSum;
}
int main() {
Node* root = new Node(10);
Node* c1 = new Node(2);
Node* c2 = new Node(10);
Node* c3 = new Node(20);
Node* c4 = new Node(1);
Node* c5 = new Node(-25);
Node* c6 = new Node(3);
Node* c7 = new Node(4);
root->children = {c1, c2, c5};
c5->children = {c6, c7};
c2->children = {c3, c4};
int result = maxPathSum(root);
std::cout << result << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Consider the two highest child path sums at each node to form the maximum path through that node.
✗ Incorrect
The maximum path sum is 48, achieved by the path 20 + 10 + 10 + 8 (from 2 and 10 children) or 20 + 10 + 10 + 8 (including the root and its two highest children). The code finds the two largest child sums to combine with the root's value.
