530. 二叉搜索树的最小绝对差
中序遍历递归实现
var pre *TreeNode
var res int
func dfs(root *TreeNode){
if root==nil{
return
}
dfs(root.Left)
if pre!=nil {
res = int(math.Min(math.Abs(float64(root.Val-pre.Val)),float64(res)))
}
pre = root
dfs(root.Right)
}
func getMinimumDifference(root *TreeNode) int {
res = math.MaxInt32
dfs(root)
return res
}
func main() {
root :=&TreeNode{Val: 1}
two :=&TreeNode{Val: 2}
three :=&TreeNode{Val: 3}
root.Right = three
three.Left = two
fmt.Println(getMinimumDifference(root))
}