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 7c83ff4 commit 8eab13aCopy full SHA for 8eab13a
Easy/Minimum Distance Between BST Nodes.java
@@ -0,0 +1,34 @@
1
+/**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ */
10
+class Solution {
11
+
12
+ int t = Integer.MAX_VALUE;
13
+ int prev = Integer.MAX_VALUE;
14
15
+ public int minDiffInBST(TreeNode root) {
16
+ inorder(root);
17
18
+ return t;
19
+ }
20
21
+ public void inorder(TreeNode root) {
22
+ if (root == null) {
23
+ return;
24
25
26
+ inorder(root.left);
27
+ if (prev != Integer.MAX_VALUE) {
28
+ t = Math.min(t, Math.abs(root.val - prev));
29
30
31
+ prev = root.val;
32
+ inorder(root.right);
33
34
+}
0 commit comments