forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_783.java
33 lines (28 loc) · 952 Bytes
/
_783.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class _783 {
public static class Solution1 {
public int minDiffInBST(TreeNode root) {
List<Integer> inorder = new ArrayList<>();
inorder(root, inorder);
return findMinDiff(inorder);
}
private int findMinDiff(List<Integer> inorder) {
int minDiff = Integer.MAX_VALUE;
for (int i = 1; i < inorder.size(); i++) {
minDiff = Math.min(minDiff, inorder.get(i) - inorder.get(i - 1));
}
return minDiff;
}
private void inorder(TreeNode root, List<Integer> inorder) {
if (root == null) {
return;
}
inorder(root.left, inorder);
inorder.add(root.val);
inorder(root.right, inorder);
}
}
}