代码随想录算法训练营day14 | leetcode 层序遍历 226.翻转二叉树 101.对称二叉树 2
层序遍历
/**
* 二叉树的层序遍历
*/
class QueueTraverse {
/**
* 存放一层一层的数据
*/
public List<List<Integer>> resList = new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
traverse(root, resList);
return resList;
}
/**
* 树的孩子节点可能有不止两个
*/
public void traverse(TreeNode node, List<List<Integer>> resList) {
// 判空
if (node == null) {
return;
}
// 准备
ArrayDeque<TreeNode> que = new ArrayDeque<>();
TreeNode cur;
// 循环开始
que.offer(node);
while (!que.isEmpty()) {
//len获取到的是每层一开始的数据大小
int len = que.size();
//对每一层的分别建立一个list存放本层数据
List<Integer> subList = new ArrayList(len);
while (len > 0) {
//取出本层节点并删除
cur = que.poll();
len--;
// visit
subList.add(cur.val);
if (cur.left != null) {
que.offer(cur.left);
}
if (cur.right != null) {
que.offer(cur.right);
}
}
//一层的数据遍历添加完毕 开始下一层
resList.add(subList);
}
}
/**
* 单纯遍历所有元素
*/
public void visit(TreeNode root) {
ArrayDeque<TreeNode> queue = new ArrayDeque<>();
TreeNode p = root,temp = null;
queue.offer(p);
while (!queue.isEmpty()) {
// visit 节点
temp = queue.poll();
System.out.println(temp.val);
if (p.left != null) {
queue.offer(temp.left);
}
if (p.right != null) {
queue.offer(temp.right);
}
}
}
}
LeetCode 226.翻转二叉树
分析1.0
仔细看题,题目中的翻转二叉树是整个树一起翻转,不是只翻转节点的左右孩子
选择遍历顺序-中序遍历(左子树翻转(左子树成了右子树),根树翻转,右子树翻转)左子树翻转了两次
class Solution {
public TreeNode invertTree(TreeNode root) {
preOrder(root);
return root;
}
public void preOrder(TreeNode root){
if(root == null){
return;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
preOrder(root.left);
preOrder(root.right);
return;
}
}
LeetCode 101.对称二叉树 2
失误 对称二叉树比较的是子树,不光是子节点
比较每个节点的左右子树是否相等,树又是由节点组成的,就是同步遍历根节点左右两颗子树,比较节点是否相等
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null){
return false;
}
return visit(root.left, root.right);
}
public boolean visit(TreeNode node1, TreeNode node2){
if(node1 == null && node2 != null){
return false;
}
if(node2 == null && node1 != null){
return false;
}
if(node1 == null && node2 == null){
return true;
}
if(node1.val != node2.val){
return false;
}
visit(node1.left, node2.left);
visit(node1.right, node2.right);
return true;
}
}
分析2.0
class Solution {
/**
* 递归法
*/
public boolean isSymmetric(TreeNode root) {
return compare(root.left, root.right);
}
private boolean compare(TreeNode left, TreeNode right) {
if (left == null && right != null) {
return false;
}
if (left != null && right == null) {
return false;
}
if (left == null && right == null) {
return true;
}
if (left.val != right.val) {
return false;
}
// 比较外侧
boolean compareOutside = compare(left.left, right.right);
// 比较内侧
boolean compareInside = compare(left.right, right.left);
return compareOutside && compareInside;
}
}
总结
- 树类题目注意区别孩子节点和子树
- 数组、字符串移除某个元素后,长度会发生变化,遍历中注意索引发生变化,树在遍历过程中的操作也会影响原本的遍历思路
- 不同的深度遍历顺序有不同的使用场景,注意具体问题具体分析
- 节点要非null才能进行操作 类似栈、队列非空才能操作一样
常用变量名增量更新
size、val、ans、cnt、cur、pre、next、left、right、index、gap、tar、res、src、len、start、end、flag、ch
代码随想录算法训练营day14 | leetcode 层序遍历 226.翻转二叉树 101.对称二叉树 2的更多相关文章
- Java实现 LeetCode 101 对称二叉树
101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2 ...
- LeetCode 101 对称二叉树的几种思路(Python实现)
对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \3 4 4 3 但是下面这个 [1,2,2 ...
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
- Leetcode题目101.对称二叉树(简单)
题目描述: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null ...
- LeetCode 101. 对称二叉树(Symmetric Tree)
题目描述 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null, ...
- LeetCode 101.对称二叉树 - JavaScript
题目描述:给定一个二叉树,检查它是否是镜像对称的. 题目分析 下面这种二叉树就是镜像对称的,符合题目要求: 1 / \ 2 2 / \ / \ 3 4 4 3 解法 1:递归检查 根据题目" ...
- 【LeetCode】101. 对称二叉树
题目 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3, ...
- LeetCode【101. 对称二叉树】
对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...
- 【leetcode 简单】第二十二题 对称二叉树
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...
- [LeetCode] N-ary Tree Level Order Traversal N叉树层序遍历
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
随机推荐
- MySQL进阶实战5,为什么查询速度会慢
一.先了解一下MySQL查询的执行过程 MySQL在查询时,它是由很多子任务组成的,每个子任务都会消耗一定的时间,如果要想优化查询,实际上要优化其子任务,可以消除一些子任务.减少子任务的执行次数.让子 ...
- CSP-S 游寄
\(\text{reflection}\) 初赛. 本来以为上午要愉快地周测,但是伟大的虎哥让我们在四楼接着练习 然后就目睹了一个万能头+return 0编译 1min30sec 的奇迹 Win7 打 ...
- python字符串常用方法介绍,基于python3.10
python字符串常用方法-目录: 1.strip().lstrip().rstrip()2.removeprefix().removesuffix()3.replace()4.split().rsp ...
- 下载Font Awesome框架
目录 一:下载Font Awesome框架 二:如何使用font awesome 1.使用图标等样式,点击复制标签即可,需要嵌套在i标签内 2.点击图标,复制标签,然后粘贴使用即可. 3.动态图片等 ...
- 常用内置模块之collections模块、时间模块、随机数random模块
今日内容回顾 目录 今日内容回顾 包的具体使用 编程思想的转变 软件开发目录规范 常用内置模块之collections模块 常用内置模块之时间模块 常用内置模块之随机数random模块 报的具体使用 ...
- snprintf拼接字符串
例如编辑一个txt文档,不断将字符输入,最终形成一个长句子.可以看成是字符串的不断拼接.snprintf函数具有这个功能. #include<stdio.h> void main(void ...
- vulnhub靶场之HACKABLE: III
准备: 攻击机:虚拟机kali.本机win10. 靶机:Hackable: III,下载地址:https://download.vulnhub.com/hackable/hackable3.ova,下 ...
- java注解基础知识整理
目录 1.注解的定义 1.1.定义一个注解 1.2.注解的使用 2.JDK内置注解 2.1.java.lang包下的注释类型 2.2.元注解 2.3.Deprecated注解 3.在注解中定义属性 3 ...
- Vue 中 Promise 的then方法异步使用及async/await 异步使用总结
转载请注明出处: 1.Promise 的 then 方法使用 then 方法是 Promise 中 处理的是异步调用,异步调用是非阻塞式的,在调用的时候并不知道它什么时候结束,也就不会等到他返回一个有 ...
- 洛谷 P1208混合牛奶 题解
一道贪心算法不是很明显的题目,其实一般的递推也可以做. 大体思路:肯定优先购买单价最低的奶农的牛奶,那么就需要先根据牛奶单价进行排序,这里用结构体会更好一点.之后在从前往后一个一个枚举,直至购买的牛奶 ...