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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package main
import (
"fmt"
)
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func max(a, b int) int {
if a > b {
return a
} else {
return b
}
}
func min(a, b int) int {
if a < b {
return a
} else {
return b
}
}
func abs(a int) int {
if a < 0 {
return -a
} else {
return a
}
}
func maxAncestorDiff(root *TreeNode) int {
result := -(1 << 30)
var walkTree func(*TreeNode, int, int)
walkTree = func(node *TreeNode, mav, miv int) {
if node == nil {
return
}
if node != root {
result = max(result, max(abs(miv-node.Val), abs(mav-node.Val)))
}
walkTree(node.Left, max(mav, node.Val), min(miv, node.Val))
walkTree(node.Right, max(mav, node.Val), min(miv, node.Val))
}
walkTree(root, result, -result)
return result
}
func main() {
fmt.Println("vim-go")
}
|