当前位置: 首页 > 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.dtcms.com/a/275991.html

相关文章:

  • springboot3X 整合高版本mybatisplus
  • pyqt5绘制矩形和线条
  • 【从零开始编写数据库:基于Python语言实现数据库ToyDB的ACID特性】
  • C语言<数据结构-单链表>(收尾)
  • Windows 开启和关闭 Administrator 用户的方法
  • 软考高级系系统分师和架构师常考知识点总结三
  • Typecho博客系统与WebSocket实时通信整合指南
  • 网络安全初级--搭建
  • GPU编程入门:CUDA与OpenCL全面解析
  • 聊下easyexcel导出
  • 岛屿数量问题
  • [爬虫实战] 多进程/多线程/协程-异步爬取豆瓣Top250
  • 小架构step系列12:单元测试
  • 【LeetCode】算法详解#8 ---螺旋矩阵
  • Linux->基础IO
  • 佩戴头盔数据集,5498张图片,平均识别率95.3% 其中戴头盔的图有2348张,支持yolo,coco json, pasical voc xml格式的标注
  • Ansible 入门指南:自动化配置管理核心技术与实战 SELinux 配置
  • day051-ansible循环、判断与jinja2模板
  • Frida绕过SSL Pinning (证书绑定)抓包;Frida注入;app无法抓包问题解决。
  • Spring之【写一个简单的IOC容器EasySpring】
  • 2025年亚太杯(中文赛项)数学建模B题【疾病的预测与大数据分析】原创论文分享
  • UE5多人MOBA+GAS 19、创建升龙技能,以及带力的被动,为升龙技能添加冷却和消耗
  • 3. java 堆和 JVM 内存结构
  • YOLOv8
  • pytables模块安装
  • 【TOOL】ubuntu升级cmake版本
  • 单细胞分析教程 | (二)标准化、特征选择、降为、聚类及可视化
  • STM32用PWM驱动步进电机
  • 快捷跑通ultralytics下的yolo系列
  • 算法第三十一天:贪心算法part05(第八章)