1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func maxAncestorDiff(root *TreeNode) int { var dfs func(node *TreeNode, minVal, maxVal int) int dfs = func(node *TreeNode, minVal, maxVal int) int { if node == nil { return maxVal - minVal } minVal = min(minVal, node.Val) maxVal = max(maxVal, node.Val) return max(dfs(node.Left, minVal, maxVal), dfs(node.Right, minVal, maxVal)) } return dfs(root, root.Val, root.Val) }
|