Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the maximum path sum starting from the current node.
DSA Javascript
function maxPathDown(node) {
if (!node) return 0;
let left = Math.max(0, maxPathDown(node.left));
let right = Math.max(0, maxPathDown(node.right));
maxSum = Math.max(maxSum, node.val + left + [1]);
return node.val + Math.max(left, right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add the right child's max path sum.
Adding node.val twice.
✗ Incorrect
The maximum path sum at the current node includes the node's value plus the maximum path sums from both left and right children. So we add 'right' to 'node.val + left'.
2fill in blank
mediumComplete the code to update the global maximum path sum variable.
DSA Javascript
let maxSum = -Infinity;
function maxPathDown(node) {
if (!node) return 0;
let left = Math.max(0, maxPathDown(node.left));
let right = Math.max(0, maxPathDown(node.right));
maxSum = Math.max(maxSum, node.val + left + right);
return node.val + Math.max(left, [1]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning node.val plus left plus right instead of max of left or right.
Returning node.val plus 0 always.
✗ Incorrect
When returning, we choose the larger path sum between left and right to continue upwards, so we add 'right' to node.val.
3fill in blank
hardFix the error in the code to correctly compute the maximum path sum in the tree.
DSA Javascript
function maxPathSum(root) {
let maxSum = -Infinity;
function maxPathDown(node) {
if (!node) return 0;
let left = Math.max(0, maxPathDown(node.left));
let right = Math.max(0, maxPathDown(node.right));
maxSum = Math.max(maxSum, node.val + left + right);
return node.val + Math.max(left, [1]);
}
maxPathDown(root);
return maxSum;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning node.val plus left plus right instead of max of left or right.
Returning node.val plus 0 always.
✗ Incorrect
The return statement should add the node's value to the maximum of left or right path sums, so 'right' is correct here.
4fill in blank
hardFill both blanks to complete the function that calculates the maximum path sum including or excluding children.
DSA Javascript
function maxPathDown(node) {
if (!node) return 0;
let left = Math.max(0, maxPathDown(node.left));
let right = Math.max(0, maxPathDown(node.right));
maxSum = Math.max(maxSum, node.val + [1] + [2]);
return node.val + Math.max(left, right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Adding node.val twice.
Adding zero instead of left or right.
✗ Incorrect
The maximum path sum at the current node includes the node's value plus both left and right maximum path sums.
5fill in blank
hardFill all three blanks to complete the dictionary comprehension that maps nodes to their maximum path sums.
DSA Javascript
const maxPathSumMap = new Map();
function maxPathDown(node) {
if (!node) return 0;
let left = Math.max(0, maxPathDown(node.left));
let right = Math.max(0, maxPathDown(node.right));
maxSum = Math.max(maxSum, node.val + left + right);
maxPathSumMap.set(node, node.val + Math.max([1], [2]));
return node.val + Math.max([3], right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using node.val inside Math.max incorrectly.
Mixing up left and right in return statement.
✗ Incorrect
We compare left and 0 to get the max non-negative path sum for the map, and return node.val plus max of left or right. So blanks are left, 0, left.
