We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8048293 commit e486cbdCopy full SHA for e486cbd
kotlin/0783-minimum-distance-between-bst-nodes.kt
@@ -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