Skip to content

Commit ff0e5b4

Browse files
committed
Time: 0 ms (100.00%), Space: 40.2 MB (35.81%) - LeetHub
1 parent dce4000 commit ff0e5b4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public int minDiffInBST(TreeNode root) {
18+
List<Integer> list= new ArrayList<>();
19+
inOrderTraversal(root, list);
20+
int min=Integer.MAX_VALUE;
21+
for(int i=0; i<list.size()-1; i++){
22+
min= Math.min(min, list.get(i+1)-list.get(i));
23+
}
24+
return min;
25+
26+
}
27+
public void inOrderTraversal(TreeNode root, List<Integer> list){
28+
if(root == null) return;
29+
inOrderTraversal(root.left, list);
30+
list.add(root.val);
31+
inOrderTraversal(root.right, list);
32+
}
33+
}

0 commit comments

Comments
 (0)