aboutsummaryrefslogtreecommitdiffstats
path: root/124/main.go
blob: fa953f1cffe6b60b4a306f8bac51542a4c388007 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main

import (
	"fmt"
	"math"
)

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

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

func walk(node *TreeNode, sum *int) int {
	if node == nil {
		return 0
	}

	left := max(0, walk(node.Left, sum))
	right := max(0, walk(node.Right, sum))
	*sum = max(*sum, left+right+node.Val)

	return max(left, right) + node.Val
}

func maxPathSum(root *TreeNode) int {
	sum := math.MinInt

	walk(root, &sum)

	return sum
}

func main() {
	fmt.Println("vim-go")
}