LeetCode-0525
102. 二叉树的层序遍历(中等)
思路:使用hash记录深度
class Solution {public List<List<Integer>> levelOrder(TreeNode root) {if(root==null)return new ArrayList<>();Map<TreeNode,Integer> deep = new HashMap<>();Queue<TreeNode> queue = new LinkedList<>();int d = deepth(root);List<List<Integer>> res = new ArrayList<>();for(int i=0;i<d;i++){res.add(new ArrayList<>());}queue.offer(root);deep.put(root,0);while(!queue.isEmpty()){TreeNode cur = queue.poll();int dp = deep.get(cur);res.get(dp).add(cur.val);if(cur.left!=null){queue.offer(cur.left);deep.put(cur.left,dp+1);}if(cur.right!=null){queue.offer(cur.right);deep.put(cur.right,dp+1);}}return res;}public int deepth(TreeNode root){if(root==null)return 0;return Math.max(deepth(root.left),deepth(root.right))+1;}
}
107. 二叉树的层序遍历 II(中等)
思路:相比刚才的解答,只需要将数组的赋值改一下即可
class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {if(root==null)return new ArrayList<>();Map<TreeNode,Integer> deep = new HashMap<>();Queue<TreeNode> queue = new LinkedList<>();int d = deepth(root);List<List<Integer>> res = new ArrayList<>();for(int i=0;i<d;i++){res.add(new ArrayList<>());}queue.offer(root);deep.put(root,0);while(!queue.isEmpty()){TreeNode cur = queue.poll();int dp = deep.get(cur);res.get(d-dp-1).add(cur.val);if(cur.left!=null){queue.offer(cur.left);deep.put(cur.left,dp+1);}if(cur.right!=null){queue.offer(cur.right);deep.put(cur.right,dp+1);}}return res;}public int deepth(TreeNode root){if(root==null)return 0;return Math.max(deepth(root.left),deepth(root.right))+1;}
}
199. 二叉树的右视图(中等)
思路:一开始想一直找最右子,然后发现有可能左子树比右子树高,这样就不对了。之后还是在层序遍历的模版上微调一下
class Solution {public List<Integer> rightSideView(TreeNode root) {if(root==null)return new ArrayList<>();Map<TreeNode,Integer> deep = new HashMap<>();Queue<TreeNode> queue = new LinkedList<>();int d = deepth(root);List<List<Integer>> res = new ArrayList<>();List<Integer> ans = new ArrayList<>();for(int i=0;i<d;i++){res.add(new ArrayList<>());}queue.offer(root);deep.put(root,0);while(!queue.isEmpty()){TreeNode cur = queue.poll();int dp = deep.get(cur);res.get(dp).add(cur.val);if(cur.left!=null){queue.offer(cur.left);deep.put(cur.left,dp+1);}if(cur.right!=null){queue.offer(cur.right);deep.put(cur.right,dp+1);}}for(int i=0;i<d;i++){List<Integer> tp = res.get(i);ans.add(tp.get(tp.size()-1));}return ans;}public int deepth(TreeNode root){if(root==null)return 0;return Math.max(deepth(root.left),deepth(root.right))+1;}
}
637. 二叉树的层平均值(简单)
思路:还是之前的模版
官方题解里面,可以在while里面for遍历队列进行处理,每一次遍历,都是同一层的元素,极好的解决了深度问题
class Solution {public List<Double> averageOfLevels(TreeNode root) {if(root==null)return new ArrayList<>();Map<TreeNode,Integer> deep = new HashMap<>();Queue<TreeNode> queue = new LinkedList<>();int d = deepth(root);List<List<Integer>> res = new ArrayList<>();List<Double> ans = new ArrayList<>();for(int i=0;i<d;i++){res.add(new ArrayList<>());}queue.offer(root);deep.put(root,0);while(!queue.isEmpty()){TreeNode cur = queue.poll();int dp = deep.get(cur);res.get(dp).add(cur.val);if(cur.left!=null){queue.offer(cur.left);deep.put(cur.left,dp+1);}if(cur.right!=null){queue.offer(cur.right);deep.put(cur.right,dp+1);}}for(int i=0;i<d;i++){List<Integer> tp = res.get(i);double sum = 0;for(int j=0;j<tp.size();j++){sum+=tp.get(j);}ans.add(sum/tp.size());}return ans;}public int deepth(TreeNode root){if(root==null)return 0;return Math.max(deepth(root.left),deepth(root.right))+1;}
}
429. N 叉树的层序遍历(中等)
思路:使用上一题官方题解的办法
class Solution {public List<List<Integer>> levelOrder(Node root) {List<List<Integer>> res = new ArrayList<>();if(root == null) return res;Queue<Node> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){List<Integer> level = new ArrayList<>();int size = queue.size(); // 这里一定要先取出size,不然中间会增加for(int i=0;i<size;i++){Node cur = queue.poll();level.add(cur.val);for(int j=0;j<cur.children.size();j++){queue.offer(cur.children.get(j));}}res.add(level);}return res;}
}
515. 在每个树行中找最大值(中等)
class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> res = new ArrayList<>();if(root == null) return res;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){int max = queue.peek().val;int size = queue.size();for(int i=0;i<size;i++){TreeNode cur = queue.poll();if(cur.val>max)max = cur.val;if(cur.left!=null)queue.offer(cur.left);if(cur.right!=null)queue.offer(cur.right);}res.add(max);}return res;}
}
116. 填充每个节点的下一个右侧节点指针
class Solution {public Node connect(Node root) {if(root==null)return root;Queue<Node> queue = new LinkedList<>();queue.add(root);while(!queue.isEmpty()){int size = queue.size();Node last = null;for(int i=0;i<size;i++){Node cur = queue.poll();if(last!=null)last.next = cur;last = cur;if(cur.left!=null)queue.offer(cur.left);if(cur.right!=null)queue.offer(cur.right);}}return root;}
}
117. 填充每个节点的下一个右侧节点指针 II(中等)
完全同上
104. 二叉树的最大深度(简单)
class Solution {public int maxDepth(TreeNode root) {if(root==null)return 0;return Math.max(maxDepth(root.left),maxDepth(root.right))+1;}
}
111. 二叉树的最小深度(简单)
class Solution {public int minDepth(TreeNode root) {if(root==null)return 0;if(root.left==null||root.right==null)return Math.max(minDepth(root.left),minDepth(root.right))+1;return Math.min(minDepth(root.left),minDepth(root.right))+1;}}
相关文章:
LeetCode-0525
102. 二叉树的层序遍历(中等) 思路:使用hash记录深度 class Solution {public List<List<Integer>> levelOrder(TreeNode root) {if(rootnull)return new ArrayList<>();Map<TreeNode,Integer> deep new HashMap&…...
【Linux 】scp命令
前言 Linux scp 命令用于 Linux 之间复制文件和目录。 scp 是 secure copy 的缩写, scp 是 linux 系统下基于 ssh 登陆进行安全的远程文件拷贝命令。 scp 是加密的,rcp 是不加密的,scp 是 rcp 的加强版。 scp命令 前言一、示例1. 从本地复制到远程2. 从…...
Docker部署yolov5
目录 环境下载源码构建Docker镜像运行docker镜像运行目标检测出现partially initialized module cv2 has no attribute _registerMatType错误出现ImportError: libSM.so.6: cannot open shared object file: No such file or directory错误出现AttributeError: Upsample object…...
如何在 Axios 中去控制 Loading?大有学问!
目录 前言 按钮loading 局部loading 全局loading 前言 loading 的展示和取消可以说是每个前端对接口的时候都要关心的一个问题。这篇文章将要帮你解决的就是如何结合axios更加简洁的处理loading展示与取消的逻辑。 首先在我们平时处理业务的时候loading一般分为三种&#x…...
充电桩检测设备厂家TK4860C交流充电桩检定装置
TK4860系列是专门针对现有交流充电桩现场检测过程中接线复杂、负载笨重、现场检测效率低等问题而研制的一系列高效检测仪器,旨在更好的开展充电桩的强制检定工作。 充电桩检测设备是一款在交流充电桩充电过程中实时检测充电电量的标准仪器,仪器以新能源…...
一文3000字实现基于Selenium+Python的web自动化测试框架
一、什么是Selenium? Selenium是一个基于浏览器的自动化测试工具,它提供了一种跨平台、跨浏览器的端到端的web自动化解决方案。Selenium主要包括三部分:Selenium IDE、Selenium WebDriver 和Selenium Grid。 Selenium IDE:Firefo…...
Android 12系统源码_窗口管理(二)WindowManager对窗口的管理过程
前言 上一篇我们具体分析了窗口管理者WindowManagerService的启动流程,对于WindowManagerService有了一个初步的认识。在此基础上,我本打算应该进一步分析WindowManagerService是如何管理系统中的各种窗口的,然而由于Android系统的架构设计,在分析WindowManagerService之前…...
python3.8,torch1.10.2+cu113、torch-geometric 安装
【1】conda create -n name python=3.8 【2】安装 torch 注意先看可适应的最高cuda版本 https://data.pyg.org/whl/ 版本对应 【3】按照顺序安装torch-geometric: torch-sparse、torch-scatter、torch-cluster、 torch-spline-conv \torch-geometric pip install torc…...
堆(heap)、栈(stack)
在程序中,栈和堆是两种非常重要的数据结构。它们都用来存储数据,但是它们的定义略有不同。 栈Stack: 栈是一种线性的数据结构,它以 “后进先出”(LIFO)的方式存储数据。栈中的内存空间在编译时就已经确定,大…...
企业级API网关之典型应用场景
目 录 01 企业面对API与网关的现状 02 APIGW介绍及企业应用场景 03 总结 01 企业面对API与网关的现状 在企业中,进行新的系统/应用/产品开发时,具有周密的流程:从需求分析、设计、开发、测试、发布与验收。所以,一…...
【2023年4月美赛加赛】Z题:The future of Olympics 25页完整论文
【2023年4月美赛加赛】Z题:The future of Olympics 25页完整论文 1 题目 背景 国际奥委会(IOC)正面临着夏季奥运会和冬季奥运会申办数量的减少**[1]**。在过去,举办奥运会的竞争非常激烈,声望也很高。然而,最近,主办…...
Rocket重试机制,消息模式,刷盘方式
一、Consumer 批量消费(推模式) Consumer端先启动 Consumer端后启动. 正常情况下:应该是Consumer需要先启动 consumer.setConsumeMessageBatchMaxSize(10);//每次拉取10条 package quickstart; import java.util.List; import co…...
linux+onenet可视化(图形化步骤)
文章目录 一、ONENET项目搭建1.1 ONENET注册1.2 创建产品与设备1.3 添加数据流 二、可视化配置 OneNET是由中国移动打造的PaaS物联网开放平台。平台能够帮助开发者轻松实现设备接入与设备连接,快速完成产品开发部署,为智能硬件、智能家居产品提供完善的物…...
汇编的基础
原视频 基础篇:1.1编程环境的安装 打开DOSBox 0.74-3 Options.bat调整窗口大小 windowresolution1200x640 outputddrawmount c D:\masm c: debugDEBUG 用Debug的R命令查看、改变CPU寄存器的内容: 用Debug的D命令查看内存中的内容: 用Debu…...
并发编程学习(十四):tomcat线程池
1、Tomcat 功能组件结构 Tomcat 的核心功能有两个,分别是负责接收和反馈外部请求的连接器 Connector,和负责处理请求的容器 Container。 其中连接器和容器相辅相成,一起构成了基本的 web 服务 Service。每个 Tomcat 服务器可以管理多个 Servi…...
简洁灵活工单管理系统,支持工单模版字段、工单状态自定义
一、开源项目简介 本项目为FeelDesk工单管理系统的开源版(OS),是基于开发者版(DEV)分离的标准版;支持工单模版字段、工单状态等自定义,可为不同的模版设置不同的路由规则;对工单需求…...
标签派单系统架构设计
需求描述 项目背景 根据员工历史成单情况,计算员工对不同类型工单的转化能力。根据员工和工单标签匹配进行派单。 业务流程图 规则描述 每10分钟,分城进行一次派单,派单规则可能会动态删减,需要支持动态配置 工单标签说明 一…...
Jmeter和Postman那个工具更适合做接口测试?
软件测试行业做功能测试和接口测试的人相对比较多。在测试工作中,有高手,自然也会有小白,但有一点我们无法否认,就是每一个高手都是从小白开始的,所以今天我们就来谈谈一大部分人在做的接口测试,小白变高手…...
k8s污点与容忍
1.前言 污点是给node节点打上污点标签,使得pod不能往该node节点上调度,污点有三种模式,分别是NoSchedule、PreferNoSchedule、NoExecute,容忍是给pod打上和node节点一样的污点标签,使pod能调度到带有该污点标签的node…...
市面上有哪些软件可以结合agentgpt的?众包平台结合的好处!
使用AgentGPT,提升工作效率! 随着科技的迅速发展,人工智能已经成为我们生活中不可或缺的一部分。而AgentGPT则是人工智能领域的一款杰出产品,它能够帮助我们提升工作效率,减少重复性劳动,让我们的生活更加便…...
[特殊字符] 智能合约中的数据是如何在区块链中保持一致的?
🧠 智能合约中的数据是如何在区块链中保持一致的? 为什么所有区块链节点都能得出相同结果?合约调用这么复杂,状态真能保持一致吗?本篇带你从底层视角理解“状态一致性”的真相。 一、智能合约的数据存储在哪里…...
stm32G473的flash模式是单bank还是双bank?
今天突然有人stm32G473的flash模式是单bank还是双bank?由于时间太久,我真忘记了。搜搜发现,还真有人和我一样。见下面的链接:https://shequ.stmicroelectronics.cn/forum.php?modviewthread&tid644563 根据STM32G4系列参考手…...
线程同步:确保多线程程序的安全与高效!
全文目录: 开篇语前序前言第一部分:线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分:synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分ÿ…...
376. Wiggle Subsequence
376. Wiggle Subsequence 代码 class Solution { public:int wiggleMaxLength(vector<int>& nums) {int n nums.size();int res 1;int prediff 0;int curdiff 0;for(int i 0;i < n-1;i){curdiff nums[i1] - nums[i];if( (prediff > 0 && curdif…...
Java - Mysql数据类型对应
Mysql数据类型java数据类型备注整型INT/INTEGERint / java.lang.Integer–BIGINTlong/java.lang.Long–––浮点型FLOATfloat/java.lang.FloatDOUBLEdouble/java.lang.Double–DECIMAL/NUMERICjava.math.BigDecimal字符串型CHARjava.lang.String固定长度字符串VARCHARjava.lang…...
大模型多显卡多服务器并行计算方法与实践指南
一、分布式训练概述 大规模语言模型的训练通常需要分布式计算技术,以解决单机资源不足的问题。分布式训练主要分为两种模式: 数据并行:将数据分片到不同设备,每个设备拥有完整的模型副本 模型并行:将模型分割到不同设备,每个设备处理部分模型计算 现代大模型训练通常结合…...
GO协程(Goroutine)问题总结
在使用Go语言来编写代码时,遇到的一些问题总结一下 [参考文档]:https://www.topgoer.com/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/goroutine.html 1. main()函数默认的Goroutine 场景再现: 今天在看到这个教程的时候,在自己的电…...
MySQL 部分重点知识篇
一、数据库对象 1. 主键 定义 :主键是用于唯一标识表中每一行记录的字段或字段组合。它具有唯一性和非空性特点。 作用 :确保数据的完整性,便于数据的查询和管理。 示例 :在学生信息表中,学号可以作为主键ÿ…...
【Linux系统】Linux环境变量:系统配置的隐形指挥官
。# Linux系列 文章目录 前言一、环境变量的概念二、常见的环境变量三、环境变量特点及其相关指令3.1 环境变量的全局性3.2、环境变量的生命周期 四、环境变量的组织方式五、C语言对环境变量的操作5.1 设置环境变量:setenv5.2 删除环境变量:unsetenv5.3 遍历所有环境…...
提升移动端网页调试效率:WebDebugX 与常见工具组合实践
在日常移动端开发中,网页调试始终是一个高频但又极具挑战的环节。尤其在面对 iOS 与 Android 的混合技术栈、各种设备差异化行为时,开发者迫切需要一套高效、可靠且跨平台的调试方案。过去,我们或多或少使用过 Chrome DevTools、Remote Debug…...
