## The question

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

Example:

```Input:

1
\
3
/
2

Output:
1

Explanation:
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
```

Note:

• There are at least two nodes in this BST.

## The solution

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 `````` ``````/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public int getMinimumDifference(TreeNode root) { } } ``````
Of this, the meat really comes in with the `getMinimumDifference` method.
So let’s use Depth First Search (`DFS`) to solve this problem.
 `````` 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 `````` ``````/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { // Store our list of points public List list = new ArrayList<>(); // Store the max differences public int dif = Integer.MAX_VALUE; // Our DFS method public void dfs(TreeNode root) { // return if not exists if(root == null) return; // recurse left dfs(root.left); // add the current node list.add(root.val); // recurse right dfs(root.right); } // Main entry point, takes in the TreeNode's root public int getMinimumDifference(TreeNode root) { // do our DFS on the tree dfs(root); // loop through our `list` for(int i=1;i