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 code that calculates the maximum path sum in a binary tree?
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def maxPathSum(root): max_sum = float('-inf') def helper(node): nonlocal max_sum if not node: return 0 left = max(helper(node.left), 0) right = max(helper(node.right), 0) current = node.val + left + right max_sum = max(max_sum, current) return node.val + max(left, right) helper(root) return max_sum # Tree structure: # -10 # / \ # 9 20 # / \ # 15 7 root = TreeNode(-10, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7))) print(maxPathSum(root))
Attempts:
2 left
💡 Hint
Consider the path that includes nodes 15, 20, and 7.
✗ Incorrect
The maximum path sum is 15 + 20 + 7 = 42. The path goes through the right subtree only, ignoring the left child 9 and root -10 for the maximum sum path.
🧠 Conceptual
intermediateUnderstanding Path Sum Components
In the maximum path sum problem, why do we use max(helper(node.left), 0) instead of just helper(node.left)?
Attempts:
2 left
💡 Hint
Think about how negative values affect sums.
✗ Incorrect
Using max(helper(node.left), 0) means if the left path sum is negative, we treat it as zero to avoid decreasing the total path sum.
🔧 Debug
advancedIdentify the Error in Maximum Path Sum Code
What error will the following code produce when calculating maximum path sum?
DSA Python
def maxPathSum(root): max_sum = float('-inf') def helper(node): if not node: return 0 left = max(helper(node.left), 0) right = max(helper(node.right), 0) current = node.val + left + right max_sum = max(max_sum, current) return node.val + max(left, right) helper(root) return max_sum
Attempts:
2 left
💡 Hint
Check variable scope inside nested functions.
✗ Incorrect
max_sum is assigned inside helper without 'nonlocal' keyword, so Python treats it as local, causing UnboundLocalError when referenced before assignment.
🚀 Application
advancedMaximum Path Sum with Negative Values
Given the binary tree below, what is the maximum path sum?
Tree:
-2
/ \
-1 -3
/ \
-4 -5
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def maxPathSum(root): max_sum = float('-inf') def helper(node): nonlocal max_sum if not node: return 0 left = max(helper(node.left), 0) right = max(helper(node.right), 0) current = node.val + left + right max_sum = max(max_sum, current) return node.val + max(left, right) helper(root) return max_sum root = TreeNode(-2, TreeNode(-1), TreeNode(-3, TreeNode(-4), TreeNode(-5))) print(maxPathSum(root))
Attempts:
2 left
💡 Hint
The maximum path sum might be a single node if all are negative.
✗ Incorrect
The maximum path sum is -1, which is the highest single node value in the tree. Paths including other nodes reduce the sum.
🧠 Conceptual
expertWhy Use Recursion for Maximum Path Sum?
Why is recursion the preferred method to solve the maximum path sum in a binary tree?
Attempts:
2 left
💡 Hint
Think about how trees are structured and how recursion fits that structure.
✗ Incorrect
Recursion fits tree structures well by processing child nodes first and combining their results, making it easy to calculate path sums from bottom to top.
