-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path783.go
More file actions
53 lines (44 loc) · 1.13 KB
/
Copy path783.go
File metadata and controls
53 lines (44 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* @Time : 2020/10/12 16:05
* @Author : cancan
* @File : 783.go
* @Function : 二叉搜索树节点最小距离
*/
/*
给定一个二叉搜索树的根节点 root,返回树中任意两节点的差的最小值。
示例:
输入: root = [4,2,6,1,3,null,null]
输出: 1
解释:
注意,root是树节点对象(TreeNode object),而不是数组。
给定的树 [4,2,6,1,3,null,null] 可表示为下图:
4
/ \
2 6
/ \
1 3
最小的差值是 1, 它是节点1和节点2的差值, 也是节点3和节点2的差值。
注意:
1.二叉树的大小范围在 2 到 100。
2.二叉树总是有效的,每个节点的值都是整数,且不重复。
*/
package QuestionBank
func minDiffInBST(root *TreeNode) int {
ans := -1
pre := -1
minDiffInBSTDfs(root, &ans, &pre)
return ans
}
func minDiffInBSTDfs(node *TreeNode, ans *int, pre *int) {
if node == nil {
return
}
minDiffInBSTDfs(node.Left, ans, pre)
if *pre != -1 && *ans == -1 {
*ans = node.Val - *pre
} else if node.Val-*pre < *ans {
*ans = node.Val - *pre
}
*pre = node.Val
minDiffInBSTDfs(node.Right, ans, pre)
}