Challenge - 5 Problems
Maximum Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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));
Attempts:
2 left
💡 Hint
Consider the path that includes the root's right subtree and its children.
✗ Incorrect
The maximum path sum is 42, coming from the path 15 -> 20 -> 7. The root's value -10 is not included because it reduces the sum. The path 15 + 20 + 7 = 42 is the highest sum path in the tree.
🧠 Conceptual
intermediate1:30remaining
Understanding Maximum Path Sum Concept
Which statement best describes the maximum path sum in a binary tree?
Attempts:
2 left
💡 Hint
Think about paths that can start and end anywhere in the tree.
✗ Incorrect
The maximum path sum is the largest sum of values from any path between two nodes in the tree. The path can start and end at any nodes and does not have to include the root or a leaf.
🔧 Debug
advanced2: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));Attempts:
2 left
💡 Hint
Check how negative values affect the path sums.
✗ Incorrect
The code does not ignore negative sums from children, so it includes negative values in the path sum. The maximum path sum is 4 from path 3 + 1, ignoring the negative left child.
❓ Predict Output
advanced1: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));
Attempts:
2 left
💡 Hint
Consider that the tree has only one node with a negative value.
✗ Incorrect
The maximum path sum is -3 because the only node has value -3. The code returns the node value since no positive gains from children exist.
🧠 Conceptual
expert1: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?
Attempts:
2 left
💡 Hint
Think about how negative values affect the sum of a path.
✗ Incorrect
Using Math.max with 0 ignores negative sums from children, preventing them from decreasing the total path sum. This ensures the path sum only includes positive contributions.