We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2475677 commit 779d046Copy full SHA for 779d046
src/0701-0800/783 - Minimum Distance Between BST Nodes/min_distance_btw_bst_nodes.go
@@ -0,0 +1,37 @@
1
+package main
2
+
3
+import "math"
4
5
+// Definition for a binary tree node.
6
+type TreeNode struct {
7
+ Val int
8
+ Left *TreeNode
9
+ Right *TreeNode
10
+}
11
12
+func minDiffInBST(root *TreeNode) int {
13
+ var (
14
+ minDiff = math.MaxInt
15
+ prev = -1
16
+ )
17
18
+ var dfs func(*TreeNode)
19
+ dfs = func(node *TreeNode) {
20
+ if node == nil {
21
+ return
22
+ }
23
24
+ dfs(node.Left)
25
26
+ if prev != -1 {
27
+ minDiff = int(math.Min(float64(minDiff), float64(node.Val-prev)))
28
29
+ prev = node.Val
30
31
+ dfs(node.Right)
32
33
34
+ dfs(root)
35
36
+ return minDiff
37
0 commit comments