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

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. 二叉树的层序遍历&#xff08;中等&#xff09; 思路&#xff1a;使用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 是加密的&#xff0c;rcp 是不加密的&#xff0c;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系列是专门针对现有交流充电桩现场检测过程中接线复杂、负载笨重、现场检测效率低等问题而研制的一系列高效检测仪器&#xff0c;旨在更好的开展充电桩的强制检定工作。 充电桩检测设备是一款在交流充电桩充电过程中实时检测充电电量的标准仪器&#xff0c;仪器以新能源…...

一文3000字实现基于Selenium+Python的web自动化测试框架

一、什么是Selenium&#xff1f; Selenium是一个基于浏览器的自动化测试工具&#xff0c;它提供了一种跨平台、跨浏览器的端到端的web自动化解决方案。Selenium主要包括三部分&#xff1a;Selenium IDE、Selenium WebDriver 和Selenium Grid。 Selenium IDE&#xff1a;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)

在程序中&#xff0c;栈和堆是两种非常重要的数据结构。它们都用来存储数据&#xff0c;但是它们的定义略有不同。 栈Stack: 栈是一种线性的数据结构&#xff0c;它以 “后进先出”&#xff08;LIFO&#xff09;的方式存储数据。栈中的内存空间在编译时就已经确定&#xff0c;大…...

企业级API网关之典型应用场景

目 录 01 企业面对API与网关的现状‍‍‍‍‍ 02 APIGW介绍及企业应用场景 03 总结 01 企业面对API与网关的现状‍ 在企业中&#xff0c;进行新的系统/应用/产品开发时&#xff0c;具有周密的流程&#xff1a;从需求分析、设计、开发、测试、发布与验收。所以&#xff0c;一…...

【2023年4月美赛加赛】Z题:The future of Olympics 25页完整论文

【2023年4月美赛加赛】Z题&#xff1a;The future of Olympics 25页完整论文 1 题目 背景 国际奥委会(IOC)正面临着夏季奥运会和冬季奥运会申办数量的减少**[1]**。在过去&#xff0c;举办奥运会的竞争非常激烈&#xff0c;声望也很高。然而&#xff0c;最近&#xff0c;主办…...

Rocket重试机制,消息模式,刷盘方式

一、Consumer 批量消费&#xff08;推模式&#xff09; Consumer端先启动 Consumer端后启动. 正常情况下&#xff1a;应该是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物联网开放平台。平台能够帮助开发者轻松实现设备接入与设备连接&#xff0c;快速完成产品开发部署&#xff0c;为智能硬件、智能家居产品提供完善的物…...

汇编的基础

原视频 基础篇&#xff1a;1.1编程环境的安装 打开DOSBox 0.74-3 Options.bat调整窗口大小 windowresolution1200x640 outputddrawmount c D:\masm c: debugDEBUG 用Debug的R命令查看、改变CPU寄存器的内容&#xff1a; 用Debug的D命令查看内存中的内容&#xff1a; 用Debu…...

并发编程学习(十四):tomcat线程池

1、Tomcat 功能组件结构 Tomcat 的核心功能有两个&#xff0c;分别是负责接收和反馈外部请求的连接器 Connector&#xff0c;和负责处理请求的容器 Container。 其中连接器和容器相辅相成&#xff0c;一起构成了基本的 web 服务 Service。每个 Tomcat 服务器可以管理多个 Servi…...

简洁灵活工单管理系统,支持工单模版字段、工单状态自定义

一、开源项目简介 本项目为FeelDesk工单管理系统的开源版&#xff08;OS&#xff09;&#xff0c;是基于开发者版&#xff08;DEV&#xff09;分离的标准版&#xff1b;支持工单模版字段、工单状态等自定义&#xff0c;可为不同的模版设置不同的路由规则&#xff1b;对工单需求…...

标签派单系统架构设计

需求描述 项目背景 根据员工历史成单情况&#xff0c;计算员工对不同类型工单的转化能力。根据员工和工单标签匹配进行派单。 业务流程图 规则描述 每10分钟&#xff0c;分城进行一次派单&#xff0c;派单规则可能会动态删减&#xff0c;需要支持动态配置 工单标签说明 一…...

Jmeter和Postman那个工具更适合做接口测试?

软件测试行业做功能测试和接口测试的人相对比较多。在测试工作中&#xff0c;有高手&#xff0c;自然也会有小白&#xff0c;但有一点我们无法否认&#xff0c;就是每一个高手都是从小白开始的&#xff0c;所以今天我们就来谈谈一大部分人在做的接口测试&#xff0c;小白变高手…...

k8s污点与容忍

1.前言 污点是给node节点打上污点标签&#xff0c;使得pod不能往该node节点上调度&#xff0c;污点有三种模式&#xff0c;分别是NoSchedule、PreferNoSchedule、NoExecute&#xff0c;容忍是给pod打上和node节点一样的污点标签&#xff0c;使pod能调度到带有该污点标签的node…...

市面上有哪些软件可以结合agentgpt的?众包平台结合的好处!

使用AgentGPT&#xff0c;提升工作效率&#xff01; 随着科技的迅速发展&#xff0c;人工智能已经成为我们生活中不可或缺的一部分。而AgentGPT则是人工智能领域的一款杰出产品&#xff0c;它能够帮助我们提升工作效率&#xff0c;减少重复性劳动&#xff0c;让我们的生活更加便…...

接口测试中缓存处理策略

在接口测试中&#xff0c;缓存处理策略是一个关键环节&#xff0c;直接影响测试结果的准确性和可靠性。合理的缓存处理策略能够确保测试环境的一致性&#xff0c;避免因缓存数据导致的测试偏差。以下是接口测试中常见的缓存处理策略及其详细说明&#xff1a; 一、缓存处理的核…...

【kafka】Golang实现分布式Masscan任务调度系统

要求&#xff1a; 输出两个程序&#xff0c;一个命令行程序&#xff08;命令行参数用flag&#xff09;和一个服务端程序。 命令行程序支持通过命令行参数配置下发IP或IP段、端口、扫描带宽&#xff0c;然后将消息推送到kafka里面。 服务端程序&#xff1a; 从kafka消费者接收…...

调用支付宝接口响应40004 SYSTEM_ERROR问题排查

在对接支付宝API的时候&#xff0c;遇到了一些问题&#xff0c;记录一下排查过程。 Body:{"datadigital_fincloud_generalsaas_face_certify_initialize_response":{"msg":"Business Failed","code":"40004","sub_msg…...

JavaScript 中的 ES|QL:利用 Apache Arrow 工具

作者&#xff1a;来自 Elastic Jeffrey Rengifo 学习如何将 ES|QL 与 JavaScript 的 Apache Arrow 客户端工具一起使用。 想获得 Elastic 认证吗&#xff1f;了解下一期 Elasticsearch Engineer 培训的时间吧&#xff01; Elasticsearch 拥有众多新功能&#xff0c;助你为自己…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院查看报告小程序

一、开发环境准备 ​​工具安装​​&#xff1a; 下载安装DevEco Studio 4.0&#xff08;支持HarmonyOS 5&#xff09;配置HarmonyOS SDK 5.0确保Node.js版本≥14 ​​项目初始化​​&#xff1a; ohpm init harmony/hospital-report-app 二、核心功能模块实现 1. 报告列表…...

2025盘古石杯决赛【手机取证】

前言 第三届盘古石杯国际电子数据取证大赛决赛 最后一题没有解出来&#xff0c;实在找不到&#xff0c;希望有大佬教一下我。 还有就会议时间&#xff0c;我感觉不是图片时间&#xff0c;因为在电脑看到是其他时间用老会议系统开的会。 手机取证 1、分析鸿蒙手机检材&#x…...

相机Camera日志分析之三十一:高通Camx HAL十种流程基础分析关键字汇总(后续持续更新中)

【关注我,后续持续新增专题博文,谢谢!!!】 上一篇我们讲了:有对最普通的场景进行各个日志注释讲解,但相机场景太多,日志差异也巨大。后面将展示各种场景下的日志。 通过notepad++打开场景下的日志,通过下列分类关键字搜索,即可清晰的分析不同场景的相机运行流程差异…...

Android Bitmap治理全解析:从加载优化到泄漏防控的全生命周期管理

引言 Bitmap&#xff08;位图&#xff09;是Android应用内存占用的“头号杀手”。一张1080P&#xff08;1920x1080&#xff09;的图片以ARGB_8888格式加载时&#xff0c;内存占用高达8MB&#xff08;192010804字节&#xff09;。据统计&#xff0c;超过60%的应用OOM崩溃与Bitm…...

提升移动端网页调试效率:WebDebugX 与常见工具组合实践

在日常移动端开发中&#xff0c;网页调试始终是一个高频但又极具挑战的环节。尤其在面对 iOS 与 Android 的混合技术栈、各种设备差异化行为时&#xff0c;开发者迫切需要一套高效、可靠且跨平台的调试方案。过去&#xff0c;我们或多或少使用过 Chrome DevTools、Remote Debug…...

零知开源——STM32F103RBT6驱动 ICM20948 九轴传感器及 vofa + 上位机可视化教程

STM32F1 本教程使用零知标准板&#xff08;STM32F103RBT6&#xff09;通过I2C驱动ICM20948九轴传感器&#xff0c;实现姿态解算&#xff0c;并通过串口将数据实时发送至VOFA上位机进行3D可视化。代码基于开源库修改优化&#xff0c;适合嵌入式及物联网开发者。在基础驱动上新增…...