package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;

/**
 * 783. Minimum Distance Between BST Nodes
 *
 * Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.

 Example :

 Input: root = [4,2,6,1,3,null,null]
 Output: 1
 Explanation:
 Note that root is a TreeNode object, not an array.

 The given tree [4,2,6,1,3,null,null] is represented by the following diagram:

      4
    /   \
   2     6
  / \
 1  3

 while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.

 Note:

 The size of the BST will be between 2 and 100.
 The BST is always valid, each node's value is an integer, and each node's value is different.
 */

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);
    }
  }
}

@@ -77,16 +77,17 @@
 eeNode;%0A
+%0A
 import j
@@ -134,733 +134,8 @@
 t;%0A%0A
-/**%0A * 783. Minimum Distance Between BST Nodes%0A *%0A * Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.%0A%0A Example :%0A%0A Input: root = %5B4,2,6,1,3,null,null%5D%0A Output: 1%0A Explanation:%0A Note that root is a TreeNode object, not an array.%0A%0A The given tree %5B4,2,6,1,3,null,null%5D is represented by the following diagram:%0A%0A      4%0A    /   %5C%0A   2     6%0A  / %5C%0A 1  3%0A%0A while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.%0A%0A Note:%0A%0A The size of the BST will be between 2 and 100.%0A The BST is always valid, each node's value is an integer, and each node's value is different.%0A */%0A%0A
 publ
@@ -150,16 +150,18 @@
  _783 %7B%0A
+  
   public
@@ -186,24 +186,28 @@
 tion1 %7B%0A    
+   
+ 
 public int m
@@ -241,16 +241,22 @@
 %7B%0A      
+     
+ 
 List%3CInt
@@ -290,16 +290,22 @@
 st%3C%3E();%0A
+      
       in
@@ -322,24 +322,30 @@
 , inorder);%0A
+      
       return
@@ -367,27 +367,35 @@
 order);%0A    
+ 
-%7D%0A%0A
+   %7D%0A%0A    
     private 
@@ -431,24 +431,30 @@
  inorder) %7B%0A
+      
       int mi
@@ -480,16 +480,22 @@
 _VALUE;%0A
+      
       fo
@@ -531,24 +531,32 @@
 e(); i++) %7B%0A
+        
         minD
@@ -619,26 +619,38 @@
  1));%0A      
-%7D%0A
+      %7D%0A      
       return
@@ -659,27 +659,35 @@
 inDiff;%0A    
-%7D%0A%0A
+    %7D%0A%0A    
     private 
@@ -735,24 +735,30 @@
  inorder) %7B%0A
+      
       if (ro
@@ -767,24 +767,32 @@
  == null) %7B%0A
+        
         retu
@@ -797,26 +797,38 @@
 turn;%0A      
+    
-%7D%0A
+  %7D%0A      
       inorde
@@ -852,24 +852,30 @@
 der);%0A      
+     
+ 
 inorder.add(
@@ -885,16 +885,22 @@
 t.val);%0A
+      
       in
@@ -931,16 +931,22 @@
 r);%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 783