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 779d046 commit 36c2030Copy full SHA for 36c2030
src/0501-0600/530 - Minimum Absolute Difference in BST/min_absolute_difference_in_bst.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 getMinimumDifference(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