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 C# code that calculates the maximum path sum in a binary tree?
DSA C#
public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { this.val = val; this.left = left; this.right = right; } } public class Solution { private int maxSum = int.MinValue; 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); } } // Tree structure: // -10 // / \ // 9 20 // / \ // 15 7 var root = new TreeNode(-10, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7)) ); var sol = new Solution(); System.Console.WriteLine(sol.MaxPathSum(root));
Attempts:
2 left
💡 Hint
Consider the path that includes the root's right subtree and both children of node 20.
✗ Incorrect
The maximum path sum is 42, coming from the path 15 -> 20 -> 7, plus the root's value -10 is not included because it reduces the sum. The path 15 + 20 + 7 = 42 is the highest sum.
🧠 Conceptual
intermediateUnderstanding Path Sum Constraints
In the Maximum Path Sum problem, which of the following statements is true about the paths considered?
Attempts:
2 left
💡 Hint
Think about whether the path must start at the root or can be anywhere.
✗ Incorrect
The maximum path sum can be any path in the tree that is connected, starting and ending at any nodes, not necessarily the root or leaf nodes.
🔧 Debug
advancedIdentify the Error in Maximum Path Sum Implementation
What error will the following C# code produce when trying to compute the maximum path sum?
DSA C#
public class Solution { private int maxSum = int.MinValue; 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 values in subtrees affect the path sum calculation.
✗ Incorrect
The code does not ignore negative gains from left or right subtrees, which can reduce the path sum. This leads to incorrect maximum path sum calculation.
📝 Syntax
advancedSyntax Error in Recursive Maximum Path Sum Function
Which option contains a syntax error in the C# recursive function for maximum path sum?
Attempts:
2 left
💡 Hint
Check for missing semicolons in the code.
✗ Incorrect
Option C is missing a semicolon after the return statement in the if condition, causing a syntax error.
🚀 Application
expertMaximum Path Sum with Negative Values
Given the binary tree below, what is the maximum path sum?
// Tree structure:
// -2
// / \
// -1 -3
// / \
// -4 -5
Use the maximum path sum algorithm that ignores negative gains.
Attempts:
2 left
💡 Hint
The maximum path sum may be a single node if all paths have negative sums.
✗ Incorrect
The maximum path sum is -1, which is the highest single node value in the tree. Since all paths have negative sums, the algorithm chooses the least negative node.
