0
0
DSA Typescriptprogramming~20 mins

Maximum Path Sum in Binary Tree in DSA Typescript - 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 TypeScript code that calculates the maximum path sum in a binary tree?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function maxPathSum(root: TreeNode | null): number {
  let maxSum = -Infinity;
  function dfs(node: TreeNode | null): number {
    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 tree = new TreeNode(-10,
  new TreeNode(9),
  new TreeNode(20, new TreeNode(15), new TreeNode(7))
);
console.log(maxPathSum(tree));
A27
B35
C42
D20
Attempts:
2 left
💡 Hint
Consider the path that includes the root's right subtree and its children.
🧠 Conceptual
intermediate
1:30remaining
Understanding Maximum Path Sum Concept
Which statement best describes the maximum path sum in a binary tree?
AIt is the largest sum of values from any path between two nodes, which may or may not include the root.
BIt is the sum of all node values in the tree.
CIt is the largest sum of values from any path starting at the root and ending at a leaf.
DIt is the largest sum of values from any path that must include the root node.
Attempts:
2 left
💡 Hint
Think about paths that can start and end anywhere in the tree.
🔧 Debug
advanced
2:00remaining
Identify the Error in Maximum Path Sum Code
What error will the following TypeScript code produce when calculating maximum path sum?
DSA Typescript
function maxPathSum(root: TreeNode | null): number {
  let maxSum = -Infinity;
  function dfs(node: TreeNode | null): number {
    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 tree = new TreeNode(1, new TreeNode(-2), new TreeNode(3));
console.log(maxPathSum(tree));
AReturns 0
BReturns 4
CReturns 1
DReturns 3
Attempts:
2 left
💡 Hint
Check how negative values affect the path sums.
Predict Output
advanced
1:30remaining
Output of Maximum Path Sum with Negative Nodes
What is the output of this TypeScript code calculating maximum path sum in a tree with negative values?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function maxPathSum(root: TreeNode | null): number {
  let maxSum = -Infinity;
  function dfs(node: TreeNode | null): number {
    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 tree = new TreeNode(-3);
console.log(maxPathSum(tree));
A-Infinity
B0
C3
D-3
Attempts:
2 left
💡 Hint
Consider that the tree has only one node with a negative value.
🧠 Conceptual
expert
1:30remaining
Why Use Math.max with 0 in Maximum Path Sum DFS?
Why does the maximum path sum algorithm use Math.max(dfs(child), 0) when calculating left and right gains?
ATo ignore negative path sums from children that would reduce the total sum.
BTo ensure the path always includes both children.
CTo convert all negative values to zero permanently in the tree.
DTo count only leaf nodes in the path sum calculation.
Attempts:
2 left
💡 Hint
Think about how negative values affect the sum of a path.