Challenge - 5 Problems
DP on Trees Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Maximum Path Sum in a Simple Binary Tree
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; let left = Math.max(dfs(node.left), 0); let 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
Consider the path that includes both left and right children plus the root.
✗ Incorrect
The maximum path sum is 2 + 1 + 3 = 6. The code calculates the maximum sum of any path in the tree, which can include both children and the root node.
❓ Predict Output
intermediateMaximum Path Sum with Negative Values
What will be the output of this code when the tree contains 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; let left = Math.max(dfs(node.left), 0); let 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
The maximum path sum can skip negative branches by using Math.max with 0.
✗ Incorrect
The maximum path sum is 15 + 20 + 7 = 42. The root's left child (9) is less than this sum, and the root's value (-10) is negative, so the path with 20 and its children gives the maximum sum.
🔧 Debug
advancedIdentify the Error in Maximum Path Sum Calculation
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; let left = dfs(node.left); let 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(-2, new TreeNode(-1), new TreeNode(-3)); console.log(maxPathSum(root));
Attempts:
2 left
💡 Hint
Check how negative values are handled in the recursion.
✗ Incorrect
The code does not ignore negative sums, so left and right can be negative, reducing the path sum. The maximum path sum calculated is -2 + (-1) + (-3) = -6.
🧠 Conceptual
advancedUnderstanding the Role of Returning Values in DFS
In the maximum path sum algorithm on trees, why do we return node.val + Math.max(left, right) from the DFS function instead of node.val + left + right?
Attempts:
2 left
💡 Hint
Think about the path shape when moving up the tree.
✗ Incorrect
The path can branch at the current node including both children, but when returning to the parent, the path can only continue through one child to avoid cycles. Hence, we return the max of left or right plus the node's value.
🚀 Application
expertMaximum Path Sum in a Tree with Multiple Branches
Given the following tree structure, what is the maximum path sum computed by the algorithm?
Tree:
10
/ \
2 10
/ \ \
20 1 -25
/ \
3 4
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; let left = Math.max(dfs(node.left), 0); let 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(2, new TreeNode(20), new TreeNode(1)), new TreeNode(10, null, new TreeNode(-25, new TreeNode(3), new TreeNode(4))) ); console.log(maxPathSum(root));
Attempts:
2 left
💡 Hint
Consider the path that includes 20, 2, 10, and the right 10 node.
✗ Incorrect
The maximum path sum is 20 + 2 + 10 + 10 = 42. The negative node -25 and its children reduce the sum, so they are ignored. The algorithm picks the path 20 -> 2 -> 10 -> 10 for the maximum sum.
