# 530 Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum [absolute difference](https://en.wikipedia.org/wiki/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:**&#x54;here are at least two nodes in this BST.

**Complexity**: O(n) time O(N) space

**The Idea**: If we run an in order traversal through the tree, the minimum difference is guaranteed to be between two adjacent elements. So that is what we do while maintaining the minimum value.

```cpp
int getMinimumDifference(TreeNode* root) {
    int prev = INT_MAX, my_min = INT_MAX;
    getMinimumDifferenceRec(root, prev, my_min);
    return my_min;
}

void getMinimumDifferenceRec(TreeNode* root, int &prev, int &my_min) {
    if (root) {
        getMinimumDifferenceRec(root->left, prev, my_min);
        my_min = min(my_min, abs(root->val - prev));
        prev = root->val;
        getMinimumDifferenceRec(root->right, prev, my_min);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/530-minimum-absolute-difference-in-bst.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
