https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    int ans;
    pair<int, int> dfs(TreeNode *cur) {
        if (cur == NULL) return {1000000, -1000000};
        pair<int, int> p = {cur -> val, cur -> val};
        pair<int, int> l = dfs(cur -> left);
        pair<int, int> r = dfs(cur -> right);
        ans = min({ans, abs(cur -> val - l.second), abs(cur -> val - r.first)});
        p.first = min({p.first, l.first, r.first});
        p.second = max({p.second, l.second, r.second});
        return p;
    }

    int getMinimumDifference(TreeNode* root) {
        ans = INT_MAX;
        dfs(root);
        return ans;
    }
};

Time Complexity: O(n)
Space: O(1)

這是還不錯的 DFS 題目,應該寫完後可以很好的感覺到 DFS 的樣子