0
0
DSA Javascriptprogramming~10 mins

Maximum Path Sum in Binary Tree in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Achild
Bparent
Cnext
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent' or 'next' instead of 'right' for the child pointer.
Leaving the property undefined.
2fill in blank
medium

Complete 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'
A-Infinity
B0
CNumber.MAX_SAFE_INTEGER
DNumber.MIN_SAFE_INTEGER
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.
3fill in blank
hard

Fix 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'
Anode.val
B0
C-Infinity
D1
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.
4fill in blank
hard

Fill 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'
A-Infinity
BmaxSum
C0
Droot.val
Attempts:
3 left
💡 Hint
Common Mistakes
Returning root.val instead of maxSum.
Initializing maxSum to 0 which fails if all values are negative.
5fill in blank
hard

Fill 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'
A-Infinity
Bleft
Cright
Dparent
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.