---
# System prepended metadata

title: 'LeetCode #530 [Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)'
tags: [Recursion, LeetCode, Easy, Tree]

---

###### tags: `LeetCode` `Recursion` `Tree` `Easy`
# LeetCode #530 [Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)
### (Easy)

```
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    priority_queue<int> pq;
    int getMinimumDifference(TreeNode* root) {
        int ans=INT_MAX;
        helper(root);
        int tmp=pq.top();pq.pop();
        while(!pq.empty()){
            ans=min(ans, tmp-pq.top());
            tmp=pq.top();pq.pop();
        }
        return ans;
    }
    void helper(TreeNode* node){
        if(!node)return;
        pq.push(node->val);
        helper(node->left);
        helper(node->right);
    }
};
```