添加 0530.二叉搜索树的最小绝对差 go版本

添加 0530.二叉搜索树的最小绝对差 go版本
This commit is contained in:
X-shuffle
2021-06-14 21:56:40 +08:00
committed by GitHub
parent 558293687c
commit a47eddd56f

View File

@ -224,8 +224,37 @@ class Solution:
return r
```
Go
> 中序遍历,然后计算最小差值
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func getMinimumDifference(root *TreeNode) int {
var res []int
findMIn(root,&res)
min:=1000000//一个比较大的值
for i:=1;i<len(res);i++{
tempValue:=res[i]-res[i-1]
if tempValue<min{
min=tempValue
}
}
return min
}
//中序遍历
func findMIn(root *TreeNode,res *[]int){
if root==nil{return}
findMIn(root.Left,res)
*res=append(*res,root.Val)
findMIn(root.Right,res)
}
```
-----------------------