Skip to content

Commit 8d59888

Browse files
authored
Create 0530-minimum-absolute-difference-in-bst.kt
1 parent e486cbd commit 8d59888

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Diff for: kotlin/0530-minimum-absolute-difference-in-bst.kt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Example:
3+
* var ti = TreeNode(5)
4+
* var v = ti.`val`
5+
* Definition for a binary tree node.
6+
* class TreeNode(var `val`: Int) {
7+
* var left: TreeNode? = null
8+
* var right: TreeNode? = null
9+
* }
10+
*/
11+
class Solution {
12+
13+
fun minDiffInBST(root: TreeNode?): Int {
14+
15+
var res = Integer.MAX_VALUE
16+
var prev = -1 //Values in range (0 to 10^5)
17+
18+
fun dfs(root: TreeNode?) {
19+
if(root == null) return
20+
21+
dfs(root.left)
22+
if(prev != -1) res = minOf(res, root.value - prev)
23+
prev = root.value
24+
dfs(root.right)
25+
}
26+
27+
dfs(root)
28+
return res
29+
}
30+
31+
val TreeNode.value get() = this.`val`
32+
}

0 commit comments

Comments
 (0)