-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_dist_bst.go
52 lines (46 loc) · 994 Bytes
/
min_dist_bst.go
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
package problem0783
import . "leetcodedaily/helpers/binarytree"
/*
Given the root of a Binary Search Tree (BST),
return the minimum difference between the values of any two different nodes in the tree.
*/
func minDiffInBST(root *TreeNode) int {
var prev, res int = -10001, 10001
traverse(root, &prev, &res)
return res
}
func traverse(root *TreeNode, prev, res *int) {
if root == nil {
return
}
traverse(root.Left, prev, res)
*res = min(*res, root.Val-*prev)
*prev = root.Val
traverse(root.Right, prev, res)
}
func minDiffInBSTStack(root *TreeNode) int {
var res int = 10001
var prev *TreeNode
var stack = []*TreeNode{}
for len(stack) > 0 || root != nil {
for root != nil {
stack = append(stack, root)
root = root.Left
}
// Pop
root = stack[len(stack)-1]
stack = stack[:len(stack)-1]
if prev != nil {
res = min(res, root.Val-prev.Val)
}
prev = root
root = root.Right
}
return res
}
func min(a, b int) int {
if a < b {
return a
}
return b
}