0
0
DSA Javascriptprogramming~20 mins

Maximum Path Sum in Binary Tree in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Maximum Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Maximum Path Sum Calculation
What is the output of the following code that calculates the maximum path sum in a binary tree?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function maxPathSum(root) {
  let maxSum = -Infinity;
  function dfs(node) {
    if (!node) return 0;
    const left = Math.max(dfs(node.left), 0);
    const right = Math.max(dfs(node.right), 0);
    maxSum = Math.max(maxSum, node.val + left + right);
    return node.val + Math.max(left, right);
  }
  dfs(root);
  return maxSum;
}

const root = new TreeNode(-10,
  new TreeNode(9),
  new TreeNode(20, new TreeNode(15), new TreeNode(7))
);
console.log(maxPathSum(root));
A20
B35
C42
D27
Attempts:
2 left
💡 Hint
Think about the path that includes the root's right subtree and its children.
🧠 Conceptual
intermediate
1:30remaining
Understanding Maximum Path Sum Definition
Which of the following best describes the maximum path sum in a binary tree?
AThe largest sum of values from any path starting at the root and ending at any leaf.
BThe sum of all node values in the tree.
CThe largest sum of values from any path that starts at a leaf and ends at the root.
DThe largest sum of values from any path between any two nodes in the tree, where the path can start and end at any node.
Attempts:
2 left
💡 Hint
The path does not have to start or end at the root or a leaf.
Predict Output
advanced
1:30remaining
Output of Maximum Path Sum with Negative Values
What is the output of this code that calculates the maximum path sum in a binary tree containing negative values?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function maxPathSum(root) {
  let maxSum = -Infinity;
  function dfs(node) {
    if (!node) return 0;
    const left = Math.max(dfs(node.left), 0);
    const right = Math.max(dfs(node.right), 0);
    maxSum = Math.max(maxSum, node.val + left + right);
    return node.val + Math.max(left, right);
  }
  dfs(root);
  return maxSum;
}

const root = new TreeNode(-3);
console.log(maxPathSum(root));
A0
B-3
C3
Dnull
Attempts:
2 left
💡 Hint
Consider that the path can be a single node.
🔧 Debug
advanced
2:00remaining
Identify the Error in Maximum Path Sum Implementation
What error will this code produce when calculating the maximum path sum?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function maxPathSum(root) {
  let maxSum = -Infinity;
  function dfs(node) {
    if (!node) return 0;
    const left = dfs(node.left);
    const right = dfs(node.right);
    maxSum = Math.max(maxSum, node.val + left + right);
    return node.val + Math.max(left, right);
  }
  dfs(root);
  return maxSum;
}

const root = new TreeNode(1, new TreeNode(2), new TreeNode(3));
console.log(maxPathSum(root));
ANo error, output is 6
BOutput is 7, which is incorrect
CStack overflow error due to infinite recursion
DOutput is 4, which is incorrect
Attempts:
2 left
💡 Hint
Check if negative values are handled correctly.
🚀 Application
expert
3:00remaining
Maximum Path Sum in a Complex Tree
Given the following binary tree, what is the maximum path sum?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

const root = new TreeNode(10,
  new TreeNode(2,
    new TreeNode(20),
    new TreeNode(1)
  ),
  new TreeNode(10,
    null,
    new TreeNode(-25,
      new TreeNode(3),
      new TreeNode(4)
    )
  )
);

function maxPathSum(root) {
  let maxSum = -Infinity;
  function dfs(node) {
    if (!node) return 0;
    const left = Math.max(dfs(node.left), 0);
    const right = Math.max(dfs(node.right), 0);
    maxSum = Math.max(maxSum, node.val + left + right);
    return node.val + Math.max(left, right);
  }
  dfs(root);
  return maxSum;
}

console.log(maxPathSum(root));
A42
B55
C48
D60
Attempts:
2 left
💡 Hint
Consider the path that includes 20, 2, 10, 10, and ignore negative branches.