diff options
Diffstat (limited to '1026')
-rw-r--r-- | 1026/go.mod | 3 | ||||
-rw-r--r-- | 1026/main.go | 60 |
2 files changed, 63 insertions, 0 deletions
diff --git a/1026/go.mod b/1026/go.mod new file mode 100644 index 0000000..73edb65 --- /dev/null +++ b/1026/go.mod @@ -0,0 +1,3 @@ +module 1026 + +go 1.19 diff --git a/1026/main.go b/1026/main.go new file mode 100644 index 0000000..c18ea2c --- /dev/null +++ b/1026/main.go @@ -0,0 +1,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") +} |