-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path783-MinimumDistanceBetweenBSTNodes.go
More file actions
99 lines (91 loc) · 2.34 KB
/
783-MinimumDistanceBetweenBSTNodes.go
File metadata and controls
99 lines (91 loc) · 2.34 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
// 783. Minimum Distance Between BST Nodes
// Given the root of a Binary Search Tree (BST),
// return the minimum difference between the values of any two different nodes in the tree.
// Example 1:
// 4
// / \
// 2 6
// / \
// 1 3
// <img src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" />
// Input: root = [4,2,6,1,3]
// Output: 1
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" />
// 1
// / \
// 0 48
// / \
// 12 49
// Input: root = [1,0,48,null,null,12,49]
// Output: 1
// Constraints:
// The number of nodes in the tree is in the range [2, 100].
// 0 <= Node.val <= 10^5
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func minDiffInBST(root *TreeNode) int {
var prev *TreeNode
res := 1 << 32 - 1
min := func (x, y int) int { if x < y { return x; }; return y; }
var visit func(root *TreeNode)
visit = func(root *TreeNode) {
if root == nil {
return
}
visit(root.Left)
if prev != nil { // inorder 中序遍历
res = min(res, (root.Val - prev.Val))
}
prev = root
visit(root.Right)
}
visit(root)
return res
}
func main() {
// Example 1:
// 4
// / \
// 2 6
// / \
// 1 3
// <img src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" />
// Input: root = [4,2,6,1,3]
// Output: 1
tree1 := &TreeNode{
4,
&TreeNode{2, &TreeNode{1, nil, nil, }, &TreeNode{3, nil, nil, }, },
&TreeNode{6, nil, nil, },
}
fmt.Println(minDiffInBST(tree1)) // 1
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" />
// 1
// / \
// 0 48
// / \
// 12 49
// Input: root = [1,0,48,null,null,12,49]
// Output: 1
tree2 := &TreeNode{
1,
&TreeNode{0, nil, nil, },
&TreeNode{48, &TreeNode{12, nil, nil, }, &TreeNode{49, nil, nil, }, },
}
fmt.Println(minDiffInBST(tree2)) // 1
}