Challenge - 5 Problems
DP Tree 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 Java code that calculates the maximum path sum in a binary tree?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int v) { val = v; } } class Solution { int maxSum = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { maxGain(root); return maxSum; } private int maxGain(TreeNode node) { if (node == null) return 0; int leftGain = Math.max(maxGain(node.left), 0); int rightGain = Math.max(maxGain(node.right), 0); int priceNewPath = node.val + leftGain + rightGain; maxSum = Math.max(maxSum, priceNewPath); return node.val + Math.max(leftGain, rightGain); } } public class Main { public static void main(String[] args) { TreeNode root = new TreeNode(-10); root.left = new TreeNode(9); root.right = new TreeNode(20); root.right.left = new TreeNode(15); root.right.right = new TreeNode(7); Solution sol = new Solution(); System.out.println(sol.maxPathSum(root)); } }
Attempts:
2 left
💡 Hint
Consider the path that includes both left and right children of node 20.
✗ Incorrect
The maximum path sum is 42, coming from the path 15 -> 20 -> 7, plus the node 20 itself, and ignoring the negative root -10 for the maximum sum path.
🧠 Conceptual
intermediateUnderstanding the Role of maxGain Function
In the DP solution for maximum path sum in a binary tree, what does the helper function maxGain(node) represent?
Attempts:
2 left
💡 Hint
Think about what the function returns to its parent call.
✗ Incorrect
maxGain(node) returns the maximum sum of a path starting at node and extending downwards to one child or none, which helps build the maximum path sum including or excluding the node.
🔧 Debug
advancedIdentify the Error in Maximum Path Sum Calculation
What error does the following code produce when calculating maximum path sum in a binary tree?
DSA Java
class Solution { int maxSum = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { maxGain(root); return maxSum; } private int maxGain(TreeNode node) { if (node == null) return 0; int leftGain = maxGain(node.left); int rightGain = maxGain(node.right); int priceNewPath = node.val + leftGain + rightGain; maxSum = Math.max(maxSum, priceNewPath); return node.val + Math.max(leftGain, rightGain); } }
Attempts:
2 left
💡 Hint
Consider how negative gains affect the path sum calculation.
✗ Incorrect
The code does not ignore negative gains by using Math.max with 0, so negative child paths reduce the sum incorrectly.
❓ Predict Output
advancedOutput of Maximum Path Sum with Negative Nodes
What is the output of the following Java code that calculates the maximum path sum in a binary tree with negative values?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int v) { val = v; } } class Solution { int maxSum = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { maxGain(root); return maxSum; } private int maxGain(TreeNode node) { if (node == null) return 0; int leftGain = Math.max(maxGain(node.left), 0); int rightGain = Math.max(maxGain(node.right), 0); int priceNewPath = node.val + leftGain + rightGain; maxSum = Math.max(maxSum, priceNewPath); return node.val + Math.max(leftGain, rightGain); } } public class Main { public static void main(String[] args) { TreeNode root = new TreeNode(-3); Solution sol = new Solution(); System.out.println(sol.maxPathSum(root)); } }
Attempts:
2 left
💡 Hint
The tree has only one node with a negative value.
✗ Incorrect
The maximum path sum is the single node value -3 because no positive gain paths exist.
🚀 Application
expertMaximum Path Sum in a Tree with Multiple Branches
Given a binary tree with nodes having values [1, 2, 3, 4, 5, 6, 7] arranged as a complete binary tree, what is the maximum path sum?
DSA Java
/* Tree structure:
1
/ \
2 3
/ \ / \
4 5 6 7
*/Attempts:
2 left
💡 Hint
Consider the path that goes through the root and both children with their maximum gains.
✗ Incorrect
The maximum path sum is 18, coming from path 5 -> 2 -> 1 -> 3 -> 7.
