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 /1026/main.go | |
parent | 872 (diff) | |
download | leetcode-b876c83c49f30e41075527522a62c3cceeb8fd0a.tar.gz leetcode-b876c83c49f30e41075527522a62c3cceeb8fd0a.zip |
1026
Diffstat (limited to '1026/main.go')
-rw-r--r-- | 1026/main.go | 60 |
1 files changed, 60 insertions, 0 deletions
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") +} |