当前位置: 首页 > 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;让我们的生活更加便…...

C++初阶-list的底层

目录 1.std::list实现的所有代码 2.list的简单介绍 2.1实现list的类 2.2_list_iterator的实现 2.2.1_list_iterator实现的原因和好处 2.2.2_list_iterator实现 2.3_list_node的实现 2.3.1. 避免递归的模板依赖 2.3.2. 内存布局一致性 2.3.3. 类型安全的替代方案 2.3.…...

React hook之useRef

React useRef 详解 useRef 是 React 提供的一个 Hook&#xff0c;用于在函数组件中创建可变的引用对象。它在 React 开发中有多种重要用途&#xff0c;下面我将全面详细地介绍它的特性和用法。 基本概念 1. 创建 ref const refContainer useRef(initialValue);initialValu…...

FFmpeg 低延迟同屏方案

引言 在实时互动需求激增的当下&#xff0c;无论是在线教育中的师生同屏演示、远程办公的屏幕共享协作&#xff0c;还是游戏直播的画面实时传输&#xff0c;低延迟同屏已成为保障用户体验的核心指标。FFmpeg 作为一款功能强大的多媒体框架&#xff0c;凭借其灵活的编解码、数据…...

SCAU期末笔记 - 数据分析与数据挖掘题库解析

这门怎么题库答案不全啊日 来简单学一下子来 一、选择题&#xff08;可多选&#xff09; 将原始数据进行集成、变换、维度规约、数值规约是在以下哪个步骤的任务?(C) A. 频繁模式挖掘 B.分类和预测 C.数据预处理 D.数据流挖掘 A. 频繁模式挖掘&#xff1a;专注于发现数据中…...

2021-03-15 iview一些问题

1.iview 在使用tree组件时&#xff0c;发现没有set类的方法&#xff0c;只有get&#xff0c;那么要改变tree值&#xff0c;只能遍历treeData&#xff0c;递归修改treeData的checked&#xff0c;发现无法更改&#xff0c;原因在于check模式下&#xff0c;子元素的勾选状态跟父节…...

【2025年】解决Burpsuite抓不到https包的问题

环境&#xff1a;windows11 burpsuite:2025.5 在抓取https网站时&#xff0c;burpsuite抓取不到https数据包&#xff0c;只显示&#xff1a; 解决该问题只需如下三个步骤&#xff1a; 1、浏览器中访问 http://burp 2、下载 CA certificate 证书 3、在设置--隐私与安全--…...

在Ubuntu中设置开机自动运行(sudo)指令的指南

在Ubuntu系统中&#xff0c;有时需要在系统启动时自动执行某些命令&#xff0c;特别是需要 sudo权限的指令。为了实现这一功能&#xff0c;可以使用多种方法&#xff0c;包括编写Systemd服务、配置 rc.local文件或使用 cron任务计划。本文将详细介绍这些方法&#xff0c;并提供…...

LLM基础1_语言模型如何处理文本

基于GitHub项目&#xff1a;https://github.com/datawhalechina/llms-from-scratch-cn 工具介绍 tiktoken&#xff1a;OpenAI开发的专业"分词器" torch&#xff1a;Facebook开发的强力计算引擎&#xff0c;相当于超级计算器 理解词嵌入&#xff1a;给词语画"…...

多模态大语言模型arxiv论文略读(108)

CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文标题&#xff1a;CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文作者&#xff1a;Sayna Ebrahimi, Sercan O. Arik, Tejas Nama, Tomas Pfister ➡️ 研究机构: Google Cloud AI Re…...

全志A40i android7.1 调试信息打印串口由uart0改为uart3

一&#xff0c;概述 1. 目的 将调试信息打印串口由uart0改为uart3。 2. 版本信息 Uboot版本&#xff1a;2014.07&#xff1b; Kernel版本&#xff1a;Linux-3.10&#xff1b; 二&#xff0c;Uboot 1. sys_config.fex改动 使能uart3(TX:PH00 RX:PH01)&#xff0c;并让boo…...