diff options
author | terminaldweller <thabogre@gmail.com> | 2022-12-09 07:39:28 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-12-09 07:39:28 +0000 |
commit | b876c83c49f30e41075527522a62c3cceeb8fd0a (patch) | |
tree | d9071289105a4120f3a662b8274f7625eb5792ba | |
parent | 872 (diff) | |
download | leetcode-b876c83c49f30e41075527522a62c3cceeb8fd0a.tar.gz leetcode-b876c83c49f30e41075527522a62c3cceeb8fd0a.zip |
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") +} |