0
0
DSA Goprogramming~10 mins

Maximum Path Sum in Binary Tree in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the TreeNode struct with integer value and pointers to left and right children.

DSA Go
type TreeNode struct {
    Val   int
    Left  *[1]
    Right *TreeNode
}
Drag options to blanks, or click blank then click option'
ATreeNode
BNode
CTree
DNodeTree
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different struct name for Left or Right pointer.
Forgetting the pointer symbol * before the type.
2fill in blank
medium

Complete the code to return the maximum of two integers a and b.

DSA Go
func max(a, b int) int {
    if a [1] b {
        return a
    }
    return b
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing minimum to be returned.
Using '==' which only checks equality.
3fill in blank
hard

Fix the error in the recursive function to compute max path sum from a node.

DSA Go
func maxPathSumHelper(root *TreeNode, maxSum *int) int {
    if root == nil {
        return 0
    }
    left := max(0, maxPathSumHelper(root.Left, maxSum))
    right := max(0, maxPathSumHelper(root.Right, maxSum))
    *maxSum = max(*maxSum, root.Val + left + [1])
    return root.Val + max(left, right)
}
Drag options to blanks, or click blank then click option'
Aleft
B0
Croot.Val
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Adding left twice instead of left and right.
Adding root.Val twice.
4fill in blank
hard

Fill both blanks to complete the main function that initializes maxSum and calls the helper.

DSA Go
func maxPathSum(root *TreeNode) int {
    maxSum := [1]
    maxPathSumHelper(root, &[2])
    return maxSum
}
Drag options to blanks, or click blank then click option'
Amath.MinInt32
BmaxSum
C0
D-100000
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing maxSum to 0 which fails for all negative nodes.
Passing maxSum instead of its address.
5fill in blank
hard

Fill all three blanks to complete the max function and import statement.

DSA Go
import (
    "fmt"
    "[1]"
)

func max(a, b int) int {
    if a [2] b {
        return a
    }
    return [3]
}

func main() {
    fmt.Println(max(10, 20))
}
Drag options to blanks, or click blank then click option'
Amath
B>
Cb
Dfmt
Attempts:
3 left
💡 Hint
Common Mistakes
Importing wrong package.
Using wrong comparison operator.
Returning a instead of b.