Challenge - 5 Problems
Maximum Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Maximum Path Sum Calculation
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 val) { this.val = val; } } public class Solution { private 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 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 the root's right subtree and both its children.
✗ Incorrect
The maximum path sum is 42, coming from the path 15 -> 20 -> 7. The root's value -10 is not included in the max path sum because it reduces the total. The path 9 alone is smaller. The code calculates max gains from left and right subtrees and updates the maxSum accordingly.
🧠 Conceptual
intermediateUnderstanding the Role of maxGain Function
In the maximum path sum algorithm for a binary tree, what is the main purpose of the maxGain function?
Attempts:
2 left
💡 Hint
Think about how the function returns values to its parent calls.
✗ Incorrect
maxGain returns the maximum sum of a path starting from the current node and extending down to one child, which helps in calculating the maximum path sum that can be formed including the current node.
🔧 Debug
advancedIdentify the Error in Maximum Path Sum Code
What error will occur when running the following code snippet for maximum path sum in a binary tree?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } } public class Solution { private int maxSum = 0; 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); } }
Attempts:
2 left
💡 Hint
Consider the initial value of maxSum and how negative values affect it.
✗ Incorrect
maxSum is initialized to 0, so if all node values are negative, maxSum will never update correctly, resulting in incorrect output. It should be initialized to Integer.MIN_VALUE to handle negative values properly.
❓ Predict Output
advancedOutput of Maximum Path Sum for Single Node Tree
What is the output of the following Java code when the binary tree has only one node with value -5?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } } public class Solution { private 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 static void main(String[] args) { TreeNode root = new TreeNode(-5); Solution sol = new Solution(); System.out.println(sol.maxPathSum(root)); } }
Attempts:
2 left
💡 Hint
The maximum path sum can be a single node value if all others are null.
✗ Incorrect
Since the tree has only one node with value -5, the maximum path sum is -5 itself. The code correctly handles negative values by initializing maxSum to Integer.MIN_VALUE.
🧠 Conceptual
expertWhy Use Math.max with 0 in maxGain Function?
In the maximum path sum algorithm, why do we use Math.max(maxGain(node.left), 0) when calculating leftGain?
Attempts:
2 left
💡 Hint
Think about how negative values affect the total path sum.
✗ Incorrect
Using Math.max with 0 ignores negative sums from subtrees, preventing them from decreasing the overall path sum. This ensures only positive contributions are added.
