---
# System prepended metadata

title: \#530 Minimum Absolute Difference in BST
tags: [leetcode, leetcode-easy]

---

# \#530 Minimum Absolute Difference in BST
## *給定一BST tree, 回傳任意兩節點之間的最小差*
## Log
 - build 20211102 by syhuang

## 初見
 - inorder traversal + 暫存陣列的前一個元素值來做差的比較即可
 - leetcode submit runtime 69%, memory 65%
```javascript=
var getMinimumDifference = function(root) {
    let prev;
    let p = root;
    let t = [];
    let result = Number.MAX_VALUE;
    
    if(!p) return result;
    
    while(p || t.length>0){
        while(p){
            t.push(p);
            p = p.left;
        }
        p = t.pop();
        if(typeof(prev)!=='undefined') result = Math.min(result, p.val - prev);
        prev = p.val;
        p = p.right;
    }
    
    return result;
};
```
## 備註
## 參考
###### tags: `leetcode`, `leetcode-easy`