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 Binary Tree
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, 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 both left and right children of node 20.
✗ Incorrect
The maximum path sum is the path 15 -> 20 -> 7, plus the node -10 is negative so not included in the max path. The path sum is 15 + 20 + 7 = 42.
🧠 Conceptual
intermediateUnderstanding the Role of MaxGain Function
In the maximum path sum algorithm on a binary tree, what does the MaxGain function return for each node?
Attempts:
2 left
💡 Hint
Think about what the function returns to its parent call.
✗ Incorrect
MaxGain returns the maximum sum of a path starting at the current node and extending down to either left or right child (or none if negative). It helps to build paths upwards.
🔧 Debug
advancedIdentify the Bug in Maximum Path Sum Code
What error will occur when running this modified maximum path sum code?
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); } } // Tree with negative values var root = new TreeNode(-2, null, new TreeNode(-3)); var sol = new Solution(); System.Console.WriteLine(sol.MaxPathSum(root));
Attempts:
2 left
💡 Hint
Check how negative gains are handled in the recursion.
✗ Incorrect
Without ignoring negative gains (no Math.Max with 0), the function sums negative values directly, so the path sum is -2 + 0 + -3 = -5.
📝 Syntax
advancedSyntax Error in Recursive Maximum Path Sum Function
Which option contains a syntax error in the MaxGain function implementation?
Attempts:
2 left
💡 Hint
Look carefully at the line endings.
✗ Incorrect
Option D is missing a semicolon after the return 0 statement, causing a syntax error.
🚀 Application
expertMaximum Path Sum in a Tree with Negative and Positive Values
Given the tree below, what is the maximum path sum computed by the DP on Trees algorithm?
DSA C#
// Tree structure: // 2 // / \ // -1 3 // / / \ // -4 5 -6 // / // 1 var root = new TreeNode(2, new TreeNode(-1, new TreeNode(-4), null), new TreeNode(3, new TreeNode(5, new TreeNode(1), null), new TreeNode(-6)) ); var sol = new Solution(); System.Console.WriteLine(sol.MaxPathSum(root));
Attempts:
2 left
💡 Hint
Consider the path 1 -> 5 -> 3 -> 2.
✗ Incorrect
The maximum path sum is 1 + 5 + 3 + 2 = 11. Negative nodes are avoided in the path.
