二叉树层序遍历(宽度优先遍历bfs--队列)
https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/solution/mian-shi-ti-32-ii-cong-shang-dao-xia-da-yin-er-c-5/
剑指 Offer 32 - II. 从上到下打印二叉树 II
public List<List<Integer>> levelOrder(TreeNode root) {//队列,遍历上一层的时候,顺便吧下一层元素加入队列Queue<TreeNode> queue = new LinkedList<>();List<List<Integer>> ans = new LinkedList<>();if (root == null) {return ans;}queue.add(root);while (!queue.isEmpty()) {List<Integer> tmp = new LinkedList<>();int lastLen = queue.size();for (int i = lastLen; i > 0; i--) {TreeNode node = queue.poll();tmp.add(node.val);if (node.left != null) {//把下一层元素放入队列queue.add(node.left);}if (node.right != null) {queue.add(node.right);}}ans.add(tmp);}return ans;}
https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/
剑指 Offer 32 - I. 从上到下打印二叉树
public int[] levelOrder(TreeNode root) {//队列Queue<TreeNode> queue = new LinkedList<>();List<Integer> list = new LinkedList<>();if (root == null) {return new int[]{};}queue.add(root);while (!queue.isEmpty()) {TreeNode node = queue.poll();list.add(node.val);if (node.left != null) {queue.add(node.left);}if (node.right != null) {queue.add(node.right);}}int[] ans = new int[list.size()];for (int i = 0; i < list.size(); i++) {ans[i] = list.get(i);}return ans;}
https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/
剑指 Offer 32 - III. 从上到下打印二叉树 III
public List<List<Integer>> levelOrder(TreeNode root) {//队列,遍历上一层的时候,顺便吧下一层元素加入队列Queue<TreeNode> queue = new LinkedList<>();List<List<Integer>> ans = new LinkedList<>();if (root == null) {return ans;}queue.add(root);int count = 1;while (!queue.isEmpty()) {List<Integer> tmp = new LinkedList<>();int lastLen = queue.size();for (int i = lastLen; i > 0; i--) {TreeNode node = queue.poll();tmp.add(node.val);if (node.left != null) {//把下一层元素放入队列queue.add(node.left);}if (node.right != null) {queue.add(node.right);}}if (count % 2 == 0) {Collections.reverse(tmp);}ans.add(tmp);count = count + 1;}return ans;}
二叉树层序遍历(宽度优先遍历bfs--队列)相关推荐
- 图的深度优先遍历和宽度优先遍历C语言,图的广度、深度优先遍历 C语言
以下是老师作为数据结构课的作业的要求,没有什么实际用处和可以探讨和总结的的地方,所以简单代码直接展示. 宽度优先遍历: #include #include #include using namespa ...
- 有苦有乐的算法 --- 图的宽度优先遍历
题目 给定一个图,使用队列对其进行宽度优先遍历 代码 public static void bfs(Node start) {if (start == null) {return;}Queue< ...
- 广度/宽度优先搜索(BFS)详解
1.前言 广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述)是连通图的一种遍历策略.因为它的思想是从一个顶点V0开始,辐射状地优先遍历其周围较广的区域,故得名. 一般可以用它做什么呢?一 ...
- 【算法入门】广度/宽度优先搜索(BFS)
广度/宽度优先搜索(BFS) [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/04/27 1.前言 广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述) ...
- 算法导论:dfs深度优先搜索算法及基于dfs的拓扑排序以及宽度优先搜索算法bfs
1.dfs深度优先搜索算法 算法导论中是通过三种标记颜色来介绍dfs的,white代表还没被搜过,grey代表被搜了一些,还没结束,white表示已经搜索完成的状态. c/c++复现dfs代码 #in ...
- (宽度优先搜索)bfs(包含所有模型)
持续更新中(点个赞吧) 首先,我们要了解什么是bfs: 宽度优先搜索算法(又称广度优先搜索)是最简便的图的搜索算法之一,这一算法也是很多重要的图的算法的原型. 其实bfs是盲目的搜索周围,知道搜到目的 ...
- 宽度优先遍历(BFS)
BFS邻接矩阵描述 #include <stdio.h> #include <malloc.h> #define VERTEXNUM 5 //队列的元素 typedef str ...
- C++笔记-基于邻接矩阵的BFS(宽度优先遍历)
邻接表是是用一个二维链表,表示顶点和顶点相邻的节点. 而邻接矩阵是,他的行,代表的是顶点,列也代表的顶点,如下: 第0行:这是第0个顶点,他可以去第1个和第2个顶点: 第1行:这是第1个顶点,他可以去 ...
- C++笔记-二维棋盘数组使用BFS(宽度优先遍历)
这里只对一个顶点只能上下左右,不能和左上,左下,右上,右下连起来. 思路步骤: 1.二维棋盘数据转链接表: 2.邻接表直接进行BFS 源码如下: #include <QDebug> #in ...
最新文章
- jquery选择器的一些处理
- Win32汇编最简ComboBox Demo
- e2200网卡驱动 linux,Linux驱动修炼之道-驱动中一些常见的宏
- 价钱转换python_如何在python中转换货币?
- SpringBoot+Shiro+ehcache实现登录失败超次数锁定帐号
- “Lephone.Data.DbEntry”的类型初始值设定项引发异常。(DbEntry.net3.9)
- 基于JDBC的数据库连接池高效管理策略
- html邮箱留言板代码,求HTML留言板代码或模板?
- ip软件在生活中器到哪些作用呢?
- 《C专家编程》:编译器的金科玉律(一)
- Js 中实现重定向的几种方式
- 如何学习游戏服务器端编程和书籍推荐
- GTK 框架(Frames)
- Oracle执行语句跟踪(1)——使用sql trace实现语句追踪
- Elasticsearch创建一个索引怎么也这么复杂
- win10局域网下利用 FlieZilla 搭建FTP服务器,实现手机平板用nplayer直接看电脑内的视频
- 五部超燃科幻电影,九成没有全部看过
- midas显示代理服务器错误,[转载]在使用桥博、midas的时候经常会遇到的问题
- 厉害!我的真我realme手机成功安装谷歌服务框架Google Play商店,安装谷歌三件套,超级简单
- R语言与数据的预处理