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 Go code that calculates the maximum path sum in a binary tree?
DSA Go
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } var maxSum int func max(a, b int) int { if a > b { return a } return b } func maxPathSumUtil(root *TreeNode) int { if root == nil { return 0 } left := maxPathSumUtil(root.Left) right := maxPathSumUtil(root.Right) left = max(left, 0) right = max(right, 0) currentSum := root.Val + left + right if currentSum > maxSum { maxSum = currentSum } return root.Val + max(left, right) } func maxPathSum(root *TreeNode) int { maxSum = -1 << 31 maxPathSumUtil(root) return maxSum } func main() { root := &TreeNode{Val: -10} root.Left = &TreeNode{Val: 9} root.Right = &TreeNode{Val: 20} root.Right.Left = &TreeNode{Val: 15} root.Right.Right = &TreeNode{Val: 7} fmt.Println(maxPathSum(root)) }
Attempts:
2 left
💡 Hint
Consider the path that includes both left and right children of the node with value 20.
✗ Incorrect
The maximum path sum is 42, coming from the path 15 -> 20 -> 7, plus the root's right subtree. The path includes nodes 15, 20, and 7, summing to 42.
❓ Predict Output
intermediateMaximum Path Sum with Negative Values
What is the output of this Go program computing maximum path sum in a tree with negative values?
DSA Go
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } var maxSum int func max(a, b int) int { if a > b { return a } return b } func maxPathSumUtil(root *TreeNode) int { if root == nil { return 0 } left := maxPathSumUtil(root.Left) right := maxPathSumUtil(root.Right) left = max(left, 0) right = max(right, 0) currentSum := root.Val + left + right if currentSum > maxSum { maxSum = currentSum } return root.Val + max(left, right) } func maxPathSum(root *TreeNode) int { maxSum = -1 << 31 maxPathSumUtil(root) return maxSum } func main() { root := &TreeNode{Val: -3} fmt.Println(maxPathSum(root)) }
Attempts:
2 left
💡 Hint
The tree has only one node with a negative value.
✗ Incorrect
Since the tree has only one node with value -3, the maximum path sum is -3 itself.
🔧 Debug
advancedIdentify the Bug in Maximum Path Sum Calculation
What error will this Go code produce when calculating maximum path sum in a binary tree?
DSA Go
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } var maxSum int func max(a, b int) int { if a > b { return a } return b } func maxPathSumUtil(root *TreeNode) int { if root == nil { return 0 } left := maxPathSumUtil(root.Left) right := maxPathSumUtil(root.Right) left = max(left, 0) right = max(right, 0) currentSum := root.Val + left + right if currentSum > maxSum { maxSum = currentSum } return root.Val + max(left, right) } func maxPathSum(root *TreeNode) int { maxSum = 0 maxPathSumUtil(root) return maxSum } func main() { root := &TreeNode{Val: -1} root.Left = &TreeNode{Val: -2} root.Right = &TreeNode{Val: -3} fmt.Println(maxPathSum(root)) }
Attempts:
2 left
💡 Hint
Check the initial value of maxSum before recursion.
✗ Incorrect
maxSum is initialized to 0, so if all path sums are less than 0, maxSum remains 0. Here, the maximum path sum is -1, but since all current sums are negative, maxSum remains 0 and returns 0 incorrectly.
🧠 Conceptual
advancedUnderstanding DP State in Maximum Path Sum on Trees
In the dynamic programming approach to find the maximum path sum in a binary tree, what does the returned value from the recursive helper function represent?
Attempts:
2 left
💡 Hint
Think about what the recursion returns to its parent call.
✗ Incorrect
The recursive function returns the maximum sum of a path starting at the current node and extending down to one child, which can be used by the parent to compute larger paths.
🚀 Application
expertMaximum Path Sum in a Tree with Multiple Negative and Positive Values
Given the following tree structure, what is the maximum path sum?
Tree:
- Root: 2
- Left child: -1
- Right child: 3
- Right child's left child: -2
- Right child's right child: 4
Use the maximum path sum definition where the path can start and end at any node.
Attempts:
2 left
💡 Hint
Consider the path 3 -> 4 and the root node 2.
✗ Incorrect
The maximum path sum is 9, coming from the path 2 -> 3 -> 4. The negative values are excluded by taking max with 0 in the recursion.
