File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ package task_783 ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .LinkedList ;
5
+ import java .util .List ;
6
+
7
+ public class Solution {
8
+
9
+ List <Integer > list = new ArrayList <>();
10
+
11
+ public int minDiffInBST (TreeNode root ) {
12
+ searchValues (root );
13
+ int minDiff = 100001 ;
14
+ for (int i = 0 ; i < list .size () - 1 ; i ++) {
15
+ minDiff = Math .min (minDiff , Math .abs (list .get (i ) - list .get (i + 1 )));
16
+ }
17
+ return minDiff ;
18
+ }
19
+
20
+ private void searchValues (TreeNode root ) {
21
+ if (root == null ) {
22
+ return ;
23
+ }
24
+ searchValues (root .left );
25
+ list .add (root .val );
26
+ searchValues (root .right );
27
+ }
28
+
29
+ }
Original file line number Diff line number Diff line change
1
+ package task_783 ;
2
+
3
+
4
+ /** Definition for a binary tree node. */
5
+ public class TreeNode {
6
+ int val ;
7
+ TreeNode left ;
8
+ TreeNode right ;
9
+
10
+ TreeNode () {
11
+ }
12
+
13
+ TreeNode (int val ) {
14
+ this .val = val ;
15
+ }
16
+
17
+ TreeNode (int val , TreeNode left , TreeNode right ) {
18
+ this .val = val ;
19
+ this .left = left ;
20
+ this .right = right ;
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments