# Find the Minimum Absolute Difference in BST using Java

## 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

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```/**
* 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) {

}
}```Code language: Java (java)```

Of this, the meat really comes in with the `getMinimumDifference` method.

So let’s use Depth First Search (`DFS`) to solve this problem.

``````/**
* 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<Integer> 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);
// 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<list.size();i++) {
// primary conditional
if(Math.abs(list.get(i) - list.get(i - 1)) < dif)
// set the difference int
dif = Math.abs(list.get(i) - list.get(i - 1));
}
// Return the difference as an integer
return dif;
}
}
```Code language: Java (java)```