当前位置: 首页 > news >正文

19th Day| 530.二叉搜索树的最小绝对差,501.二叉搜索树中的众数, 236.二叉树的最近公共祖先

LeetCode 530 二叉搜索树的最小绝对差

题目链接:530.二叉树搜索树的最小绝对差

class Solution {int min = Integer.MAX_VALUE;TreeNode pre; public int getMinimumDifference(TreeNode root) {traversal(root);return min;}public void traversal(TreeNode root){if(root == null) return;traversal(root.left);if(pre != null && (root.val - pre.val) < min){min = root.val - pre.val;}pre = root;traversal(root.right);}
}

LeetCode 501 二叉搜索树中的众数

题目链接:501.二叉搜索树中的众数

class Solution {TreeNode pre;int maxTimes = 1;int currentTimes;List<Integer> list = new ArrayList<>();public int[] findMode(TreeNode root) {if(root == null) return new int[]{};traversal(root);int[] res = new int[list.size()];for(int i = 0; i < list.size(); i++){res[i] = list.get(i);}return res;}public void traversal(TreeNode root){if(root == null) return;traversal(root.left);if(pre == null || root.val != pre.val){currentTimes = 1;}if(pre != null && root.val == pre.val){currentTimes+=1;}if(currentTimes > maxTimes){list.clear();maxTimes = currentTimes;list.add(root.val);}else if(currentTimes == maxTimes){list.add(root.val);}pre = root;traversal(root.right);}
}

LeetCode 236 二叉树的最近公共祖先

题目链接:236.二叉树的最近公共祖先

class Solution {public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {if(root == null) return null;if(root.val == p.val || root.val == q.val) return root;TreeNode left = lowestCommonAncestor(root.left, p ,q);TreeNode right = lowestCommonAncestor(root.right, p, q);if(left != null && right != null) return root;if(left == null && right != null) return right;if(left != null && right == null) return left;return null;}
}

http://www.lryc.cn/news/586478.html

相关文章:

  • 电子基石:硬件工程师的器件手册 (五) - 三极管:电流放大的基石与开关的利刃
  • 敏捷开发方法全景解析
  • ABSD(基于架构的软件开发)深度解析:架构驱动的工程范式
  • day051-ansible循环、判断与jinja2模板
  • java进阶(一)+学习笔记
  • (一)一阶数字低通滤波器---原理及其推导
  • 前后端分离项目的完整部署(Jenkins自动化部署)
  • 什么是数据库同步软件?为什么要关注数据库同步技术?
  • 阻有形,容无声——STA 签核之RC Corner
  • 【MaterialDesign】谷歌Material(Google Material Icons) 图标英文 对照一览表
  • Kotlin文件
  • AI大模型(七)Langchain核心模块与实战(二)
  • Java SE--抽象类和接口
  • Linux系统编程——目录 IO
  • JavaScript:移动端特效--从触屏事件到本地存储
  • 一文理解缓存的本质:分层架构、原理对比与实战精粹
  • 深入理解设计模式之工厂模式:创建对象的艺术
  • Cypress与多语言后端集成指南
  • 数据结构——散列表
  • 为什么有些PDF无法复制文字?原理分析与解决方案
  • Cursor创建Spring Boot项目
  • 指令微调时,也要考虑提示损失
  • 《Java Web程序设计》实验报告六 JSP+JDBC+MySQL实现登录注册
  • 多表查询-4-外连接
  • xml映射文件的方式操作mybatis
  • 2025 全球酒店用品厂家竞争力排行榜发布:扬州卓韵领衔,布草工厂实力重塑行业格局
  • 使用iso制作ubuntu22.04docker镜像
  • 【数据结构初阶】--单链表(一)
  • ICCV2025 特征点检测 图像匹配 RIPE
  • dify 用postman调试参数注意