0
0
DSA Goprogramming~20 mins

Maximum Path Sum in Binary Tree in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Maximum Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Maximum Path Sum Calculation
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 maxPathSum(root *TreeNode) int {
    maxSum = -1 << 31
    maxGain(root)
    return maxSum
}

func maxGain(node *TreeNode) int {
    if node == nil {
        return 0
    }
    leftGain := max(maxGain(node.Left), 0)
    rightGain := max(maxGain(node.Right), 0)
    priceNewPath := node.Val + leftGain + rightGain
    if priceNewPath > maxSum {
        maxSum = priceNewPath
    }
    return node.Val + max(leftGain, rightGain)
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

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))
}
A27
B35
C42
D20
Attempts:
2 left
💡 Hint
Consider the path that includes nodes 15, 20, and 7 plus the root's right subtree.
🧠 Conceptual
intermediate
1:30remaining
Understanding Maximum Path Sum Definition
Which of the following best describes the maximum path sum in a binary tree?
AThe largest sum of values from any path starting at the root and ending at any leaf.
BThe largest sum of values from any path starting at a leaf and ending at the root.
CThe sum of all node values in the tree.
DThe largest sum of values from any path between any two nodes, where the path may or may not pass through the root.
Attempts:
2 left
💡 Hint
The path can start and end at any nodes, not necessarily the root or leaves.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Maximum Path Sum Code
What error will the following Go code produce when calculating 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 maxPathSum(root *TreeNode) int {
    maxSum = 0
    maxGain(root)
    return maxSum
}

func maxGain(node *TreeNode) int {
    if node == nil {
        return 0
    }
    leftGain := max(maxGain(node.Left), 0)
    rightGain := max(maxGain(node.Right), 0)
    priceNewPath := node.Val + leftGain + rightGain
    if priceNewPath > maxSum {
        maxSum = priceNewPath
    }
    return node.Val + max(leftGain, rightGain)
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

func main() {
    root := &TreeNode{Val: -3}
    fmt.Println(maxPathSum(root))
}
ARuntime panic due to nil pointer dereference
BReturns 0 instead of -3 due to incorrect maxSum initialization
CCompilation error due to missing return statement
DReturns -3 correctly
Attempts:
2 left
💡 Hint
Check the initial value of maxSum and how it affects negative values.
📝 Syntax
advanced
1:30remaining
Identify Syntax Error in Maximum Path Sum Code
Which option contains a syntax error in the Go code for maximum path sum?
DSA Go
package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

var maxSum int

func maxPathSum(root *TreeNode) int {
    maxSum = -1 << 31
    maxGain(root)
    return maxSum
}

func maxGain(node *TreeNode) int {
    if node == nil {
        return 0
    }
    leftGain := max(maxGain(node.Left), 0)
    rightGain := max(maxGain(node.Right), 0)
    priceNewPath := node.Val + leftGain + rightGain
    if priceNewPath > maxSum {
        maxSum = priceNewPath
    }
    return node.Val + max(leftGain, rightGain)
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

func main() {
    root := &TreeNode{Val: 1}
    root.Left = &TreeNode{Val: 2}
    root.Right = &TreeNode{Val: 3}
    fmt.Println(maxPathSum(root))
}
ANo syntax error; code compiles successfully
BIncorrect variable declaration for maxSum
CUsing '<<' operator incorrectly for negative number initialization
DMissing closing brace in maxGain function
Attempts:
2 left
💡 Hint
Check if all braces and declarations are correct.
🚀 Application
expert
2:30remaining
Maximum Path Sum in Complex Tree Structure
Given the following binary tree, what is the maximum path sum?
DSA Go
Tree structure:
        -2
       /  \
      -1   3
          / \
         4  -5
        / 
       2
A9
B6
C4
D3
Attempts:
2 left
💡 Hint
Consider paths that include positive nodes and avoid negative sums where possible.