Complete the code to define the TreeNode struct with integer value and pointers to left and right children.
type TreeNode struct {
Val int
Left *[1]
Right *TreeNode
}The TreeNode struct has Left and Right fields that are pointers to TreeNode type.
Complete the code to return the maximum of two integers a and b.
func max(a, b int) int { if a [1] b { return a } return b }
To find the maximum, return a if a is greater than b, else return b.
Fix the error in the recursive function to compute max path sum from a node.
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)
}The max path sum through the current node includes root.Val plus left and right max sums.
Fill both blanks to complete the main function that initializes maxSum and calls the helper.
func maxPathSum(root *TreeNode) int {
maxSum := [1]
maxPathSumHelper(root, &[2])
return maxSum
}Initialize maxSum to the smallest integer to handle negative values. Pass its address to helper.
Fill all three blanks to complete the max function and import statement.
import ( "fmt" "[1]" ) func max(a, b int) int { if a [2] b { return a } return [3] } func main() { fmt.Println(max(10, 20)) }
Import math package. Use '>' to compare a and b. Return b if a is not greater.