Challenge - 5 Problems
DP on Trees Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Maximum Path Sum in a Simple Binary Tree
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 root = new TreeNode(1, new TreeNode(2), new TreeNode(3)); console.log(maxPathSum(root));
Attempts:
2 left
💡 Hint
Think about the path that includes both left and right children plus the root.
✗ Incorrect
The maximum path sum is 2 + 1 + 3 = 6. The function calculates the max sum including left and right subtrees and updates the global max.
❓ Predict Output
intermediate2:00remaining
Maximum Path Sum with Negative Values
What is the output of this code when the tree contains 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 root = new TreeNode(-10, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7))); console.log(maxPathSum(root));
Attempts:
2 left
💡 Hint
Consider the path that includes nodes 15, 20, and 7.
✗ Incorrect
The maximum path sum is 15 + 20 + 7 = 42. The function ignores negative paths by using Math.max with 0.
🔧 Debug
advanced2:00remaining
Identify the Error in Maximum Path Sum Implementation
What error will this code produce when run?
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 = 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));
Attempts:
2 left
💡 Hint
Check how negative values are handled in recursion.
✗ Incorrect
No runtime error and output is 6 (correct for this positive tree). However, the code has a logical bug: it does not ignore negative subtree sums (missing Math.max(..., 0)), which would cause incorrect results on trees with negatives.
🧠 Conceptual
advanced1:30remaining
Understanding the Role of Math.max in DP on Trees
Why do we use Math.max(dfs(node.left), 0) in the maximum path sum calculation?
Attempts:
2 left
💡 Hint
Think about how negative values affect sums.
✗ Incorrect
Using Math.max with 0 ignores negative sums, preventing them from reducing the total path sum.
🚀 Application
expert3:00remaining
Maximum Path Sum in a Tree with Multiple Branches
Given the following tree structure, what is the maximum path sum?
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; } } 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: 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; } console.log(maxPathSum(root));
Attempts:
2 left
💡 Hint
Focus on the path that includes nodes 20, 2, 10, and 10.
✗ Incorrect
The maximum path sum is 42 from the path 20 -> 2 -> 10 -> 10. The negative branch (-25) is ignored due to Math.max with 0.