youngyangyang04 / leetcode-master-comment

用来做评论区
0 stars 0 forks source link

[Vssue]0530.二叉搜索树的最小绝对差.md #60

Open youngyangyang04 opened 5 months ago

youngyangyang04 commented 5 months ago

https://www.programmercarl.com/0530.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E7%BB%9D%E5%AF%B9%E5%B7%AE.html

Du1in9 commented 4 months ago
class Solution {
    private List<Integer> list = new ArrayList<>();

    public int getMinimumDifference(TreeNode root) {
        inorderTraversal(root);
        int res = list.get(1) - list.get(0);
        for(int i = 2; i < list.size(); i++){
            res = Math.min(list.get(i) - list.get(i - 1), res);
        }
        return res;
    }
}