aboutsummaryrefslogtreecommitdiffstats
path: root/783/main.go
blob: 089c86e7fe520a2c5bc5c65524e86fd83cb0ca8d (plain) (blame)
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
package main

import (
	"fmt"
	"math"
)

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func walk(node *TreeNode, vals *[]int) {
	if node == nil {
		return
	}

	walk(node.Left, vals)
	*vals = append(*vals, node.Val)
	walk(node.Right, vals)
}

func minDiffInBST(root *TreeNode) int {
	var vals []int

	walk(root, &vals)

	minDist := math.MaxInt32

	for i := 1; i < len(vals); i++ {
		minDist = min(minDist, vals[i]-vals[i-1])
	}

	return minDist
}

func main() {
	fmt.Println("vim-go")
}