代码随想录训练营Day18

31 阅读1分钟

530.二叉搜索树的最小绝对差 leetcode.com/problems/mi…

class Solution {
    TreeNode pre;// 记录上一个遍历的结点
    int result = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
       if(root==null)return 0;
       traversal(root);
       return result;
    }
    public void traversal(TreeNode root){
        if(root==null)return;
        //左
        traversal(root.left);
        //中
        if(pre!=null){
            result = Math.min(result,root.val-pre.val);
        }
        pre = root;
        //右
        traversal(root.right);
    }
}


501.二叉搜索树中的众数 TODO

  1. 二叉树的最近公共祖先

  2. 二叉搜索树的最近公共祖先