算法训练营day19--530.二叉搜索树的最小绝对差+501.二叉搜索树中的众数+236. 二叉树的最近公共祖先

一、530.二叉搜索树的最小绝对差

题目链接:https://leetcode.cn/problems/minimum-absolute-difference-in-bst/

文章讲解:https://programmercarl.com/0530.二叉搜索树的最小绝对差.html

视频讲解:https://www.bilibili.com/video/BV1DD4y11779

1.1 初见思路

  1. 二叉搜索树的定义:左子树的所有元素的值都小于中节点,右子树的所有元素的值都大于中节点
  2. 二叉搜索树的特性:二叉搜索树的中序遍历后的数组是一个有序数组

1.2 具体实现

java 复制代码
class Solution {
    TreeNode pre;
    int res = Integer.MAX_VALUE;

    public int getMinimumDifference(TreeNode root) {
        getMin(root);
        return res;
    }

    public void getMin(TreeNode node) {
        if (node == null) {
            return;
        }
        getMin(node.left);
        if (pre != null) {
            res = Math.min(res, Math.abs(node.val - pre.val));
        }

        pre = node;
        getMin(node.right);
    }
}

1.3 重难点

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

题目链接:https://leetcode.cn/problems/find-mode-in-binary-search-tree/

文章讲解:https://programmercarl.com/0501.二叉搜索树中的众数.html

视频讲解:https://www.bilibili.com/video/BV1fD4y117gp

2.1 初见思路

  1. 转换成有序数组,然后遍历一遍数组就可以得到结果
  2. 可以在遍历树的时候就得到结果

2.2 具体实现

java 复制代码
class Solution {

    int times = 0;
    int maxTimes = 0;
    TreeNode pre;
    List<Integer> res = new ArrayList<Integer>();

    public int[] findMode(TreeNode root) {
        findRes(root);
        int[] resArr = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            resArr[i] = res.get(i);
        }
        return resArr;
    }

    // 中序遍历:左中右
    public void findRes(TreeNode node) {
        if (node == null) {
            return;
        }

        findRes(node.left);
        if (pre == null || pre.val != node.val) {
            times = 1;
        }
        if (pre != null && pre.val == node.val) {
            times++;
        }
        if (times > maxTimes) {
            maxTimes = times;
            res.clear();
            res.add(node.val);
        } else if (times == maxTimes) {
            res.add(node.val);
        }
        pre = node;
        findRes(node.right);

    }

}

2.3 重难点

三、 236. 二叉树的最近公共祖先

题目链接:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/

文章讲解:https://programmercarl.com/0236.二叉树的最近公共祖先.html

视频讲解:https://www.bilibili.com/video/BV1jd4y1B7E2

3.1 初见思路

  1. 无思路

3.2 具体实现

java 复制代码
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
      if(root==null || root==p || root==q){
          return root;
      }
      TreeNode left = lowestCommonAncestor(root.left,p,q);
      TreeNode right = lowestCommonAncestor(root.right,p,q);
      if(left==null){
          return right;
      }
      if(right==null){
          return left;
      }
      return root;

    }

}

3.3 重难点

相关推荐
yidaqiqi19 分钟前
[目标检测] YOLO系列算法讲解
算法·yolo·目标检测
飞天狗11123 分钟前
2024 山东省ccpc省赛
c++·算法
卡尔曼的BD SLAMer36 分钟前
计算机视觉与深度学习 | Python实现EMD-SSA-VMD-LSTM-Attention时间序列预测(完整源码和数据)
python·深度学习·算法·cnn·lstm
珊瑚里的鱼1 小时前
【滑动窗口】LeetCode 1658题解 | 将 x 减到 0 的最小操作数
开发语言·c++·笔记·算法·leetcode·stl
落樱弥城2 小时前
角点特征:从传统算法到深度学习算法演进
人工智能·深度学习·算法
共享家95272 小时前
哈希的原理、实现
c++·算法
进击的小白菜2 小时前
用Java实现单词搜索(LeetCode 79)——回溯算法详解
java·算法·leetcode
珂朵莉MM2 小时前
2024 睿抗机器人开发者大赛CAIP-编程技能赛-专科组(国赛)解题报告 | 珂学家
开发语言·人工智能·算法·leetcode·职场和发展·深度优先·图论
小智学长 | 嵌入式3 小时前
进阶-数据结构部分:2、常用排序算法
java·数据结构·算法
少了一只鹅3 小时前
字符函数和字符串函数
c语言·算法