代码随想录算法训练营day15 | 102. 二叉树的层序遍历,226. 翻转二叉树,101. 对称二叉树

news/2024/11/18 21:31:29/

目录

102. 二叉树的层序遍历

226. 翻转二叉树

101. 对称二叉树

100. 相同的树


100是101的衍生题目。572也为101的衍生题目。

102. 二叉树的层序遍历

思路:

 以前的笔记

代码:

class Solution {public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> list = new ArrayList<List<Integer>>();if (root == null) {return list;}Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.offer(root);while (!queue.isEmpty())  {List<Integer> levelList = new LinkedList<>();int len = queue.size();// 遍历这一层for (int i = 0; i < len; i++) {TreeNode node = queue.poll();levelList.add(node.val);if (node.left != null) {queue.offer(node.left);}if (node.right != null) {queue.offer(node.right);}}list.add(levelList);}return list;}
}

226. 翻转二叉树

思路:

        本题目可以使用递归,迭代和层序遍历来做。需要注意的是迭代的方法中,只能使用前序和后序遍历,因为使用中序遍历会对同一个子树翻转两次。

代码:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*///  DFS递归方法
class Solution {public TreeNode invertTree(TreeNode root) {if (root == null) {return root;}swapChildren(root);invertTree(root.left);invertTree(root.right);return root;}public void swapChildren(TreeNode node) {TreeNode tem = node.left;node.left = node.right;node.right = tem;}
}// 迭代的前序遍历
class Solution {public TreeNode invertTree(TreeNode root) {if (root == null) {return root;}Stack<TreeNode> stack = new Stack<>();stack.push(root);while (!stack.isEmpty()) {TreeNode node = stack.pop();swapChildren(node);if (node.right != null) {stack.push(node.right);}if (node.left != null) {stack.push(node.left);}}return root;}public void swapChildren(TreeNode node) {TreeNode tem = node.left;node.left = node.right;node.right = tem;}
}// 层序遍历
class Solution {public TreeNode invertTree(TreeNode root) {if (root == null) {return root;}Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);while (!queue.isEmpty()) {TreeNode node = queue.poll();swapChildren(node);if (node.left != null) {queue.offer(node.left);}if (node.right != null) {queue.offer(node.right);}}return root;}public void swapChildren(TreeNode node) {TreeNode tem = node.left;node.left = node.right;node.right = tem;}
}

101. 对称二叉树

思路:

        dfs:判断一个树是否对称,从根节点开始,看左子树和右子树是否对称。】

        bfs:通过队列进行遍历比较,把左子树根节点和右子树根节点入队列,比较因素同dfs。需要注意的是,后续入队列时,左子树的左节点和右子树的右节点,左子树的右节点和右子树的左节点一起入队,两两入队列,两两出队列比较。

代码:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/// dfs
class Solution {public boolean isSymmetric(TreeNode root) {if (root == null) {return true;}return compare(root.left, root.right);}// 判断一个节点的左子树和右子树是否对称public boolean compare(TreeNode left, TreeNode right) {// 左子树和右子树都为空,对称if (left == null && right == null) {return true;}// 左右子树只有一个为空,不对称if (left == null || right == null) {return false;}// 左子树根节点值不等于右子树根节点值,不对称if (left.val != right.val) {return false;}// 只剩下left.val和right.val相等的情况// 需要判断left.left和right.right, left.right和right.left是否对称// boolean outside = compare(left.left, right.right);// boolean inside = compare(left.right, right.left);// return outside && inside;// 简化return compare(left.left, right.right) && compare(left.right, right.left);}
}// bfs
class Solution {public boolean isSymmetric(TreeNode root) {if (root == null) {return true;}Queue<TreeNode> queue = new LinkedList<>();queue.offer(root.right);queue.offer(root.left);while (!queue.isEmpty()) {TreeNode right = queue.poll();TreeNode left = queue.poll();if (left == null && right == null) {continue;}if (left == null || right == null) {return false;}if (left.val != right.val) {return false;}// left.val等于right.valqueue.offer(left.left);queue.offer(right.right);queue.offer(left.right);queue.offer(right.left);}return true;}
}

这两道题目基本和本题是一样的,只要稍加修改就可以AC。

  • 100.相同的树(opens new window)
  • 572.另一个树的子树

100. 相同的树

 代码:

// dfs
class Solution {public boolean isSameTree(TreeNode p, TreeNode q) {if (p == null && q == null) {return true;}if (p == null || q == null) {return false;}if (p.val != q.val) {return false;} // p.val和q.val相等的情况return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);}
}// bfs
class Solution {public boolean isSameTree(TreeNode p, TreeNode q) {if (p == null && q == null) {return true;}Queue<TreeNode> queue = new LinkedList<>();queue.offer(p);queue.offer(q);while (!queue.isEmpty()) {TreeNode left = queue.poll();TreeNode right = queue.poll();if (left == null && right == null) {continue;}if (left == null || right == null) {return false;}if (left.val != right.val) {return false;}queue.offer(left.left);queue.offer(right.left);queue.offer(left.right);queue.offer(right.right);}return true;}
}


http://www.ppmy.cn/news/988116.html

相关文章

无涯教程-jQuery - Pulsate方法函数

Pulsate 效果可以与effect()方法一起使用。这会使元素的不透明性产生多次脉冲。 Pulsate - 语法 selector.effect( "pulsate", {arguments}, speed ); 这是所有参数的描述- times - 脉动的时间。默认值为3。model - 效果的模式。可以是"显示(show)"&a…

2023 7-29

题目1 删除排序链表重复元素 思路和代码 /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *n…

小白学算法一IQR

写在前面 1&#xff1a; IQR算法介绍 2&#xff1a;IQR可以用来做什么呢 一&#xff1a;IQR算法介绍 假设你的朋友们一起参加了一场马拉松比赛&#xff0c;这些朋友们跑完全程马拉松所用的时间&#xff08;单位&#xff1a;分钟&#xff09;如下&#xff1a; 90, 94, 96, 97,…

【计算机视觉 | 图像分割】arxiv 计算机视觉关于图像分割的学术速递(7 月 26 日论文合集)

文章目录 一、分割|语义相关(7篇)1.1 Learning Transferable Object-Centric Diffeomorphic Transformations for Data Augmentation in Medical Image Segmentation1.2 Optical Flow boosts Unsupervised Localization and Segmentation1.3 Spectrum-guided Multi-granularity…

启动Anaconda卡在loading applications的解决办法

启动Anaconda卡在 loading applications的解决办法 问题解决方法 问题 系统环境&#xff1a;macOS BigSur v11.2.2 启动anaconda后&#xff0c;卡在 loading applications界面。 解决方法 在anaconda安装目录下找到conda_api.py文件&#xff0c;将 data yaml.load(f)修改为…

20230728----重返学习-跨域-模块化-webpack初步

day-122-one-hundred-and-twenty-two-20230728-跨域-模块化-webpack初步 跨域 跨域 为什么要跨域&#xff1f; 浏览器为了安全&#xff0c;不能让我们的html文件可以随意引用别的服务器中的文件&#xff0c;只允许我们的html或js文件中&#xff0c;请求我们自己服务器。这个…

Cisco 路由器配置管理

大多数网络中断的最常见原因是错误的配置更改。对网络设备配置的每一次更改都伴随着造成网络中断、安全问题甚至性能下降的风险。计划外更改使网络容易受到意外中断的影响。 Network Configuration Manager 网络更改和配置管理 &#xff08;NCCM&#xff09;解决方案&#xff…

贼全! 一举通关的 Spring+SpringBoot+SpringCloud 全攻略, 是真香啊

前几天&#xff0c;有幸从朋友那里得到了一份 Alibaba 内部的墙裂推荐的“玩转 Spring 全家桶的 PDF”&#xff0c;我也不是个吝啬的人&#xff0c;好的东西当然要一起分享。那今天我就秀一把&#xff0c;带你一站通关 Spring、Spring Boot 与 Spring Cloud,让你轻松斩获大厂 O…