Challenge - 5 Problems
DP on Trees 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 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 dfs(node): nonlocal max_sum if not node: return 0 left = max(dfs(node.left), 0) right = max(dfs(node.right), 0) max_sum = max(max_sum, node.val + left + right) return node.val + max(left, right) dfs(root) return max_sum root = TreeNode(1, TreeNode(2), TreeNode(3)) print(maxPathSum(root))
Attempts:
2 left
💡 Hint
Think about the path that includes both left and right children plus the root.
✗ Incorrect
The maximum path sum is 2 + 1 + 3 = 6. The path includes left child, root, and right child.
❓ Predict Output
intermediateMaximum Path Sum with Negative Values
What is the output of this code that finds the maximum path sum in a tree with negative values?
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 dfs(node): nonlocal max_sum if not node: return 0 left = max(dfs(node.left), 0) right = max(dfs(node.right), 0) max_sum = max(max_sum, node.val + left + right) return node.val + max(left, right) dfs(root) return max_sum root = TreeNode(-10, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7))) print(maxPathSum(root))
Attempts:
2 left
💡 Hint
Consider the path that includes 15, 20, and 7.
✗ Incorrect
The maximum path sum is 15 + 20 + 7 = 42. The path does not include the root -10 because it reduces the sum.
🔧 Debug
advancedIdentify the Error in Maximum Path Sum Implementation
What error does the following code produce when calculating maximum path sum?
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 dfs(node): if not node: return 0 left = dfs(node.left) right = dfs(node.right) max_sum = max(max_sum, node.val + left + right) return node.val + max(left, right) dfs(root) return max_sum root = TreeNode(1, TreeNode(2), TreeNode(3)) print(maxPathSum(root))
Attempts:
2 left
💡 Hint
Check variable scope inside nested functions.
✗ Incorrect
max_sum is assigned inside dfs without 'nonlocal' keyword, so Python treats it as local but it is used before assignment causing UnboundLocalError.
🧠 Conceptual
advancedUnderstanding DP State in Maximum Path Sum on Trees
In the DP solution for maximum path sum in a binary tree, what does the return value of the recursive function represent?
Attempts:
2 left
💡 Hint
Think about what the function returns to its parent call.
✗ Incorrect
The recursive function returns the maximum gain from the current node down to one child path, which can be extended by the parent.
🚀 Application
expertMaximum Path Sum in a Tree with Multiple Branches (N-ary Tree)
Given an N-ary tree where each node can have multiple children, which approach correctly computes the maximum path sum where the path can start and end at any nodes?
Attempts:
2 left
💡 Hint
Consider how to extend the binary tree approach to multiple children.
✗ Incorrect
For N-ary trees, we find max gains from all children, pick top two to form a path through current node, update max sum, and return max gain to parent.
