代码随想录训练营二刷第十五天 | 层序遍历10道 226.翻转二叉树 101.对称二叉树 2
代码随想录训练营二刷第十五天 | 层序遍历10道 226.翻转二叉树 101.对称二叉树 2
一、102. 二叉树的层序遍历
题目链接:https://leetcode.cn/problems/binary-tree-level-order-traversal/
思路:两次while,内层控制每一行的数量,数量是添加的子节点的个数。
class Solution {public List<List<Integer>> levelOrder(TreeNode root) {LinkedList<TreeNode> queue = new LinkedList<>();List<List<Integer>> result = new ArrayList<>();if (root == null) return result;queue.add(root);while (!queue.isEmpty()) {List<Integer> list = new ArrayList<>();int num = queue.size();while (num > 0) {TreeNode node = queue.poll();list.add(node.val);num--;if (node.left != null) {queue.add(node.left);}if (node.right != null) {queue.add(node.right);}}result.add(list);}return result;}
}
二、107. 二叉树的层序遍历 II
题目链接:https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/
思路:正常的层序遍历,结束后翻转数组。
class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> arrayLists = new ArrayList<>();LinkedList<TreeNode> queue = new LinkedList<>();if (root == null) return arrayLists;queue.add(root);while (!queue.isEmpty()) {int len = queue.size();List<Integer> list = new ArrayList<>();while (len > 0) {TreeNode node = queue.poll();list.add(node.val);len--;if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}arrayLists.add(list);}int i = 0, j = arrayLists.size() - 1;while (i < j) {List<Integer> temp = arrayLists.get(i);arrayLists.set(i, arrayLists.get(j));arrayLists.set(j, temp);i++;j--;}return arrayLists;}
}
三、199. 二叉树的右视图
题目链接:https://leetcode.cn/problems/binary-tree-right-side-view/
思路:正常层序遍历,只有每层最后一个才加入结果集。
class Solution {public List<Integer> rightSideView(TreeNode root) {List<Integer> list = new ArrayList<>();LinkedList<TreeNode> queue = new LinkedList<>();if (root == null) return list;queue.add(root);while (!queue.isEmpty()) {int len = queue.size();while (len > 0) {TreeNode node = queue.poll();if (len == 1) list.add(node.val);len--;if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}}return list;}
}
四、637. 二叉树的层平均值
题目链接:https://leetcode.cn/problems/average-of-levels-in-binary-tree/
思路:正常层序遍历。
class Solution {public List<Double> averageOfLevels(TreeNode root) {List<Double> list = new ArrayList<>();LinkedList<TreeNode> queue = new LinkedList<>();queue.add(root);while (!queue.isEmpty()) {int len = queue.size();Double sum = 0.0;for (int i = 0; i < len; i++) {TreeNode node = queue.poll();sum += node.val;if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}list.add(sum / len);}return list;}
}
五、429. N 叉树的层序遍历
题目链接:https://leetcode.cn/problems/n-ary-tree-level-order-traversal/
思路:常规层序遍历注意空指针。
class Solution {public List<List<Integer>> levelOrder(Node root) {List<List<Integer>> arrayLists = new ArrayList<>();LinkedList<Node> queue = new LinkedList<>();if (root == null) return arrayLists;queue.add(root);while (!queue.isEmpty()) {int len = queue.size();List<Integer> list = new ArrayList<>();for (int i = 0; i < len; i++) {Node node = queue.poll();list.add(node.val);for (Node child : node.children) {if (child != null) queue.add(child);}}arrayLists.add(list);}return arrayLists;}
}
六、515. 在每个树行中找最大值
题目链接:https://leetcode.cn/problems/find-largest-value-in-each-tree-row/
思路:正常层序遍历。
class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> list = new ArrayList<>();LinkedList<TreeNode> queue = new LinkedList<>();if (root == null) return list;queue.add(root);while (!queue.isEmpty()) {int len = queue.size(), max = Integer.MIN_VALUE;for (int i = 0; i < len; i++) {TreeNode node = queue.poll();max = Math.max(max, node.val);if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}list.add(max);}return list;}
}
七、116. 填充每个节点的下一个右侧节点指针
题目链接:https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/
思路:层序遍历
class Solution {public Node connect(Node root) {if (root == null) return root;LinkedList<Node> queue = new LinkedList<>();queue.add(root);while (!queue.isEmpty()) {int len = queue.size();while (len > 0) {Node node = queue.poll();if (len > 1) node.next = queue.peek();len--;if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}}return root;}
}
八、117. 填充每个节点的下一个右侧节点指针 II
题目链接:https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/
思路:和上一题一样。
class Solution {public Node connect(Node root) {if (root == null) return root;LinkedList<Node> queue = new LinkedList<>();queue.add(root);while (!queue.isEmpty()) {int len = queue.size();while (len > 0) {Node node = queue.poll();if (len > 1) node.next = queue.peek();len--;if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}}return root;}
}
九、104. 二叉树的最大深度
题目链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/
思路:层序遍历。
class Solution {public int maxDepth(TreeNode root) {if (root == null) return 0;LinkedList<TreeNode> queue = new LinkedList<>();queue.add(root);int deep = 0;while (!queue.isEmpty()) {int len = queue.size();for (int i = 0; i < len; i++) {TreeNode node = queue.poll();if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}deep++;}return deep;}
}
十、111. 二叉树的最小深度
题目链接:https://leetcode.cn/problems/minimum-depth-of-binary-tree/
思路:层序遍历提早返回。
class Solution {public int minDepth(TreeNode root) {if (root == null) return 0;LinkedList<TreeNode> queue = new LinkedList<>();int deep = 0;queue.add(root);while (!queue.isEmpty()) {int len = queue.size();for (int i = 0; i < len; i++) {TreeNode node = queue.poll();if (node.left == null && node.right == null) {return ++deep;}if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}deep++;}return deep;}
}
十一、226.翻转二叉树
题目链接:https://leetcode.cn/problems/invert-binary-tree/
思路:前序遍历交换
class Solution {public TreeNode invertTree(TreeNode root) {reserve(root);return root;}void reserve(TreeNode node) {if (node == null)return;TreeNode temp = node.left;node.left = node.right;node.right = temp;reserve(node.left);reserve(node.right);}
}
十二、101.对称二叉树 2
题目链接:https://leetcode.cn/problems/symmetric-tree/
思路:把一棵树分成两颗树进行比较
class Solution {boolean flag = true;public boolean isSymmetric(TreeNode root) {equal(root.left, root.right);return flag;}void equal(TreeNode node1, TreeNode node2) {if (node1 == null && node2 == null) {}else if (node1 != null && node2 != null) {if (node1.val != node2.val) {flag = false;}else {equal(node1.left, node2.right);equal(node1.right, node2.left);}}else {flag = false;}}
}
相关文章:
![](https://www.ngui.cc/images/no-images.jpg)
代码随想录训练营二刷第十五天 | 层序遍历10道 226.翻转二叉树 101.对称二叉树 2
代码随想录训练营二刷第十五天 | 层序遍历10道 226.翻转二叉树 101.对称二叉树 2 一、102. 二叉树的层序遍历 题目链接:https://leetcode.cn/problems/binary-tree-level-order-traversal/ 思路:两次while,内层控制每一行的数量,…...
![](https://img-blog.csdnimg.cn/b6c3ede5db8f4567ac99b2d19785e72e.png)
nowcoder NC10 大数乘法
题目链接: https://www.nowcoder.com/practice/c4c488d4d40d4c4e9824c3650f7d5571?tpId196&tqId37177&rp1&ru/exam/company&qru/exam/company&sourceUrl%2Fexam%2Fcompany&difficultyundefined&judgeStatusundefined&tags&tit…...
![](https://img-blog.csdnimg.cn/9995c09e57304520bb91227069a502d9.png)
非科班菜鸡算法学习记录 | 代码随想录算法训练营第58天|| 单调栈! 739. 每日温度 496.下一个更大元素 I
739. 每日温度 输入一个数组,找比i天温度高的第一天 知识点:单调栈 状态:看思路自己写 思路: 看自己写的注释,维护一个单调栈 // 版本一 class Solution { public:vector<int> dailyTemperatures(vector<…...
![](https://www.ngui.cc/images/no-images.jpg)
【Luogu】 P5445 [APIO2019] 路灯
题目链接 点击打开链接 题目解法 转化很妙 考虑关路灯 x x x 的操作 令左边第一个未关的路灯为 L L L,右边第一个未关的路灯为 R R R,那么这一次会影响的区间即为 l ∈ [ L 1 , x ] , r ∈ [ x , R − 1 ] l\in[L1,x],\;r\in[x,R-1] l∈[L1,x],…...
![](https://img-blog.csdnimg.cn/828a4eaea77345b6a0bd97a36427807b.png)
Kafka3.0.0版本——消费者(独立消费者消费某一个主题中某个分区数据案例__订阅分区)
目录 一、独立消费者消费某一个主题中某个分区数据案例1.1、案例需求1.2、案例代码1.3、测试 一、独立消费者消费某一个主题中某个分区数据案例 1.1、案例需求 创建一个独立消费者,消费firstTopic主题 0 号分区的数据,所下图所示: 1.2、案…...
![](https://img-blog.csdnimg.cn/2dd0e5933e6b4c66b146183af6d50ea7.png)
基于Simulink的用于电力系统动态分析
💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️座右铭&a…...
![](https://img-blog.csdnimg.cn/66124a20217146aa8871e624fb4e6a4f.png)
日200亿次调用,喜马拉雅网关的架构设计
说在前面 在40岁老架构师 尼恩的读者社区(50)中,很多小伙伴拿到一线互联网企业如阿里、网易、有赞、希音、百度、滴滴的面试资格。 最近,尼恩指导一个小伙伴简历,写了一个《API网关项目》,此项目帮这个小伙拿到 字节/阿里/微博/…...
![](https://img-blog.csdnimg.cn/b6464fa9a64143fc8e2cf7ee07a73736.png)
构造函数和析构函数(个人学习笔记黑马学习)
构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作。 #include <iostream> using namespace std;//对象初始化和清理class…...
![](https://img-blog.csdnimg.cn/img_convert/7167b6975a3fbc83ba14509e5b91e6ce.png)
GPT引领前沿与应用突破之GPT4科研实践技术与AI绘图教程
详情点击链接:GPT引领前沿与应用突破之GPT4科研实践技术与AI绘图教程 前沿 GPT对于每个科研人员已经成为不可或缺的辅助工具,不同的研究领域和项目具有不同的需求。 如在科研编程、绘图领域: 1、编程建议和示例代码: 无论你使用的编程语言是…...
![](https://www.ngui.cc/images/no-images.jpg)
Git上传新项目
第一步:初始化 Git 仓库 首先,打开终端或命令行界面,然后导航到项目目录。运行下面的命令来初始化一个新的 Git 仓库: git init这将创建一个新的 .git 子目录,其中包含了初始化的 Git 仓库。 第二步:添加…...
![](https://www.ngui.cc/images/no-images.jpg)
C语言文件操作总结
目录 字符方式读入文件 数据块方式读写文件 文件定位与随机读写 文件中数据的修改 字符方式读入文件 1.向文件中写入(输入字符) 用 fputc 函数或 puts 函数可以把一个字符写到磁盘文件中去。 int fputc(int ch,FILE * fp) ch 是要输出的字符&#…...
![](https://www.ngui.cc/images/no-images.jpg)
原生js之dom如何进行事件监听(事件捕获/冒泡)
那么好,这次主要讲解的就是dom是如何进行事件监听和事件取消监听的,我们知道vue中主要用watch来进行监听. js监听与取消监听 那么原生js主要用到的就是addListenEvent事件来进行监听,可以监听文档dom对象也可以监听浏览器bom对象,监听事件的语法结构如下 Dom/Bom监听 eleme…...
![](https://img-blog.csdnimg.cn/9caeb6a74fed4897b0475424b3aa5a0d.png)
使用SimPowerSystems并网光伏阵列研究(Simulink实现)
💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️座右铭&a…...
![](https://img-blog.csdnimg.cn/5268adda713b4d178f0150e34c4a28c7.png)
BUUCTF-WEB-[ACTF2020 新生赛]Includel
打开靶机 点击tips 利用Burp抓包,未见异常 但发现了响应头是 PHP/7.3.13 想到了"php://input"伪协议POST发送PHP代码 构建Payload:?filephp://filter/readconvert.base64-encode/resourceflag.php 这里需要注意的是使用php://filter伪协议…...
![](https://www.ngui.cc/images/no-images.jpg)
算法通关村十四关:白银挑战-堆能高效解决的经典问题
白银挑战-堆能高效解决的经典问题 1.在数组中找第K大的元素 LeetCode215 https://leetcode.cn/problems/kth-largest-element-in-an-array/ 思路分析 主要解决方法有3个,选择法,堆查找法和快速排序法 方法1:选择法 先遍历一遍找到最大的…...
![](https://img-blog.csdnimg.cn/img_convert/15199501875d5d9d8ab0fc608ed416e4.png)
跨站请求伪造(CSRF)攻击与防御原理
跨站请求伪造(CSRF) 1.1 CSRF原理 1.1.1 基本概念 跨站请求伪造(Cross Site Request Forgery,CSRF)是一种攻击,它强制浏览器客户端用户在当前对其进行身份验证后的Web 应用程序上执行非本意操作的攻击&a…...
![](https://img-blog.csdnimg.cn/d8e4a69bf99246f69d840ba270b02e2a.png)
从0到1实现播放控制器
这系列文章主要讲诉如何从0到1使用QT实现带时间显示、滚动字幕等的自定义配置视频播放控制器。平时我们乘坐地铁经常看到各条线的播放控制器都大同小异。其实都是通过QT等界面开发软件来实现的。 在具体开发之前,需要明确我们需要做什么? 1. 开发一个可…...
![](https://www.ngui.cc/images/no-images.jpg)
【Vue-Element-Admin】导出el-table全部数据
背景 因为el-table实现了分页查询,所以想要实现el-table需要重新编写一个查询全部数据的方法 查询全部数据 listQuery: export default{return{listQuery:{//page:1,//limit:20,//如果想兼容按条件导出,可以定义查询条件age:undefined,sex:undefined…...
![](https://www.ngui.cc/images/no-images.jpg)
MFC 更改控件的大小和位置
获取当前主窗体的位置rect CRect dlgNow;GetWindowRect(&dlgNow);获取某一个控件当前的位置 CRect rect;CButton* pBtn (CButton*)GetDlgItem(IDC_BUTTONXXX);//获取按钮控件pBtn->GetWindowRect(rect);CWnd* pWnd(CWnd*)GetDlgItem(IDC_EDITXXX);//其它控件࿰…...
![](https://www.ngui.cc/images/no-images.jpg)
【向量数据库】相似向量检索Faiss数据库的安装及余弦相似度计算(C++)
目录 简介安装方法安装OpenBLAS安装lapack编译Faiss 代码示例余弦相似度计算输出ID号而非索引的改进版 简介 Faiss 是一个强大的向量相似度搜索库,具有以下优点: 高效的搜索性能:Faiss 在处理大规模向量数据时表现出色。它利用了高度优化的索…...
![](https://img-blog.csdnimg.cn/img_convert/b457bca1f7220a8b70c1df09bc1d3621.jpeg)
教育培训小程序的设计与功能解析
随着互联网的发展,线上教育逐渐成为一种趋势,越来越多的人开始选择在线学习。而搭建一个适合自己的线上教育小程序,可以为教育机构或个人提供更好的教学和学习体验。在本文中,我们将介绍如何通过一个第三方制作平台来搭建在线教育…...
![](https://img-blog.csdnimg.cn/b0baeb92f7094c87b449abd910ec0279.png)
【ES】illegal_argument_exception“,“reason“:“Result window is too large
查询ES数据返回错误: {"root_cause":[{"type":"illegal_argument_exception","reason":"Result window is too large, from size must be less than or equal to: [10000] but was [999999]. See the scroll api for…...
![](https://www.ngui.cc/images/no-images.jpg)
SpringBoot实现登录拦截
如果我们不进行登录拦截的话,即使我们跳过登录页面直接去访问任意一个页面也能访问成功,那么登录功能就没有意义,同时也会存在安全问题,因为有些操作是要用户登录后才能执行的,如果用户没有登录,该接口就获…...
![](https://img-blog.csdnimg.cn/img_convert/3a617f0e84c6961d26d9cd5623859c53.png)
浅谈泛在电力物联网、能源互联网与虚拟电厂
导读:从能源互联网推进受阻,到泛在电力物联网名噪一时,到虚拟电厂再次走向火爆,能源领域亟需更进一步的数智化发展。如今,随着新型电力系统建设推进,虚拟电厂有望迎来快速发展。除了国网和南网公司下属的电…...
![](https://www.ngui.cc/images/no-images.jpg)
深度学习框架安装与配置指南:PyTorch和TensorFlow详细教程
如何安装和配置深度学习框架PyTorch和TensorFlow 为什么选择PyTorch和TensorFlow?PyTorchTensorFlow安装PyTorch 步骤1:安装Python步骤2:使用pip安装PyTorch 安装TensorFlow 步骤1:安装Python步骤2:使用pip安装TensorF…...
![](https://www.ngui.cc/images/no-images.jpg)
vue中属性执行顺序
vue中属性的执行顺序 在Vue 2中,组件的生命周期和数据绑定的执行顺序如下: data:首先,组件会调用 data 函数,该函数返回一个对象,该对象的属性和方法会被分配给组件的 $data。init:接下来&…...
![](https://img-blog.csdnimg.cn/5c49a3b1e0ff4963895ee721734b2a82.png)
【代码随想录】Day 50 动态规划11 (买卖股票Ⅲ、Ⅳ)
买卖股票Ⅲ https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/ 无语了。。。 写的很好就是怎么都过不了。。。 还是就用代码随想录的写法吧。。。 class Solution { public:int maxProfit(vector<int>& prices) {int n prices.size();vector&…...
![](https://img-blog.csdnimg.cn/bf79f06da8b141e0bb133c40e233554b.png)
PHP反序列化漏洞
一、序列化,反序列化 序列化:将php对象压缩并按照一定格式转换成字符串过程反序列化:从字符串转换回php对象的过程目的:为了方便php对象的传输和存储 seriallize() 传入参数为php对象,序列化成字符串 unseriali…...
![](https://img-blog.csdnimg.cn/98dbf341637349b5aacf0fb58db2048b.png)
容器编排学习(一)k8s集群管理
一 Kubernetes 1 概述 就在Docker容器技术被炒得热火朝天之时,大家发现,如果想要将Docker应用于具体的业务实现,是存在困难的一一编排、管理和调度等各个方面,都不容易。于是,人们迫切需要一套管理系统࿰…...
![](https://www.ngui.cc/images/no-images.jpg)
js去除字符串空格的几种方式
方法1:(最常用)全部去除掉空格 var str abc d e f g ; function trim(str) { var reg /[\t\r\f\n\s]*/g; if (typeof str string) { var trimStr str.replace(reg,); } console.lo…...
![](/images/no-images.jpg)
成都优化网站哪家公司好/手机制作网站的软件
远程服务器返回错误: 404错误、远程服务器返回错误:500错误、 HttpWebResponse远程服务器返回错误:(404、500) 错误。 现象 我们编码实现请求一个页面时,请求的代码类似如下代码: HttpWebRequest req (HttpWebRequest)WebRequest.Create(strUrl);req.Us…...
![](/images/no-images.jpg)
电子商务网站建设讯息/快速网站排名优化
tomcat超时解问题 在eclipse启动tomcat时遇到超时45秒的问题: 错误:Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 错误提示就是我们限定了部署的时间导致的错误。改动 workspace\.metadata\.plugins\org.eclipse.wst.…...
![](https://img-blog.csdnimg.cn/img_convert/08cd494acfa269d7394262af5fd696bb.png)
邳州做网站的公司/b2b网站免费推广
于戴尔笔记本怎么重装系统呢?其实关于戴尔笔记本怎么重装系统的方法小编也是说过的了,但是还是有不少的用户不知道戴尔笔记本电脑怎么重装系统,接下来是小编为大家收集的dell2900如何重装系统,欢迎大家阅读。dell2900如何重装系统第一步、设…...
![](https://img-blog.csdnimg.cn/2019061519140579.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1FpbjE5OTk=,size_16,color_FFFFFF,t_70)
公司邮箱后缀正确的是以下/佛山做优化的公司
出于某些原因不得不安装opencv。索性就写一个博客,以备不时之需。 opencv的安装环境:win10专业版 担心其他人安装的时候可能会出错,所以提醒一下。但是一般情况下这个安装环境是没有影响的。 参考链接: VS2017配置opencv教程&am…...
![](/images/no-images.jpg)
如何设计网站的首页/网站推广哪家好
下载到SlideMenu的源码,打开例子,发现有些错误,先把依赖的包给导入发现在BaseActivity有几个红叉,提示不能使用getActionBar...什么的分析一下其使用过程,首先 BaseActivity extends SlidingFragmentActivity为了兼容性…...
![](/images/no-images.jpg)
158百事通做网站是诈骗吗/网络推广工作是做什么的
考风较差也是目前较为突出的问题,同时也影响学风,为此,计算中心采取了以下措施加强考试管理,并在学生中赢得了考试最严的口碑。1.制作了考试须知,通过网站和任课教师向考生宣传。2.每次考试均有…...