Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the TreeNode constructor function.
DSA Javascript
function TreeNode(val) {
this.val = val;
this.left = null;
this.[1] = null;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent' or 'next' instead of 'right' for the child pointer.
Leaving the property undefined.
✗ Incorrect
The TreeNode has 'left' and 'right' pointers to child nodes. Here, the blank should be 'right' to complete the node structure.
2fill in blank
mediumComplete the code to initialize the maxSum variable to the smallest possible number.
DSA Javascript
let maxSum = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 which may be incorrect if all values are negative.
Using Number.MAX_SAFE_INTEGER which is very large positive number.
✗ Incorrect
We initialize maxSum to -Infinity to ensure any path sum will be larger and update maxSum correctly.
3fill in blank
hardFix the error in the recursive function to calculate max path sum from a node.
DSA Javascript
function maxGain(node) {
if (node === null) return 0;
let leftGain = Math.max(maxGain(node.left), [1]);
let rightGain = Math.max(maxGain(node.right), 0);
let priceNewpath = node.val + leftGain + rightGain;
maxSum = Math.max(maxSum, priceNewpath);
return node.val + Math.max(leftGain, rightGain);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.val instead of 0 which is incorrect here.
Using negative infinity which doesn't help avoid negative sums.
✗ Incorrect
We use 0 as the minimum gain from left or right child to avoid negative sums reducing the path.
4fill in blank
hardFill both blanks to complete the main function that returns the maximum path sum in the binary tree.
DSA Javascript
var maxPathSum = function(root) {
maxSum = [1];
function maxGain(node) {
if (node === null) return 0;
let leftGain = Math.max(maxGain(node.left), 0);
let rightGain = Math.max(maxGain(node.right), 0);
let priceNewpath = node.val + leftGain + rightGain;
maxSum = Math.max(maxSum, priceNewpath);
return node.val + Math.max(leftGain, rightGain);
}
maxGain(root);
return [2];
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning root.val instead of maxSum.
Initializing maxSum to 0 which fails if all values are negative.
✗ Incorrect
Initialize maxSum to -Infinity to handle negative values. Return maxSum after traversal to get the maximum path sum.
5fill in blank
hardFill all three blanks to complete the code that calculates the maximum path sum in a binary tree.
DSA Javascript
var maxPathSum = function(root) {
let maxSum = [1];
function maxGain(node) {
if (node === null) return 0;
let leftGain = Math.max(maxGain(node.[2]), 0);
let rightGain = Math.max(maxGain(node.[3]), 0);
let priceNewpath = node.val + leftGain + rightGain;
maxSum = Math.max(maxSum, priceNewpath);
return node.val + Math.max(leftGain, rightGain);
}
maxGain(root);
return maxSum;
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent' instead of 'left' or 'right' for child nodes.
Initializing maxSum to 0 which fails for all negative nodes.
✗ Incorrect
Initialize maxSum to -Infinity. Use node.left and node.right to traverse children correctly.