LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal
题目描述:
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree{3,9,20,#,#,15,7},3
/ \
9 20
/ \
15 7return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
题目解答:
/**
* 实质是二叉树的广度优先搜索
* 利用一个辅助队列保存被访问的当前节点的左右孩子,
* 调整进出队列的顺序以实现层序遍历
*/
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result=new ArrayList<>(); if(root==null){
return result;
} /**
* 一般来说,队列的实现选择LinkedList即可
* 队列保存节点就不用找什么变量了
*/
Queue<TreeNode> queue=new LinkedList<>();
queue.offer(root);
//首先把头结点的值保存到结果集,然后把左右子节点分别进入队列
while(!queue.isEmpty()){//需要使用isEmptyy判断,不能使用null
List<Integer> temp=new ArrayList<>();
/**
* error!这里对进行循环的过程,队列长度是在不断变化的
* size需要等于队列出队前的长度
*/
int size =queue.size();
for(int i=0;i<size;i++){
TreeNode node=queue.poll();
temp.add(node.val);
if(node.left!=null)
queue.offer(node.left);
if(node.right!=null)
queue.offer(node.right);
}
//当前队列全部poll,到这里已经完成了一层的遍历
result.add(temp);
}
return result;
}
LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树的更多相关文章
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- LeetCode 107. Binary Tree Level Order Traversal II (二叉树阶层顺序遍历之二)
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 102. Binary Tree Level Order Traversal ------层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to righ ...
- [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- Java for LeetCode 107 Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode(32)-Binary Tree Level Order Traversal
题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...
- (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
随机推荐
- Java工程为什么要加一个biz层
biz是Business的缩写,实际上就是控制层(业务逻辑层).解释:控制层的主要作用就是协调model层和view层直接的调用和转换.能够有效的避免请求直接进行数据库内容调用,而忽略了逻辑处理的部分 ...
- linux下编译qt5.6.0静态库——configure配置
linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...
- python中单元测试/数据库预处理的技巧
假设文件结构: pkg/ __init__.py components/ core.py __init__.py tests/ core_test.py __init__.py python -m 你 ...
- 如何解决phpcms后台验证码不显示的问题
方法一: 主要在于是否开启gd库 查看办法 找到php.ini文件 搜索extension=php_gd2.dll这段代码(windows) 然后把前面的;符号去掉即可. centOS6.5中可能需要 ...
- sublime配置markdown
1.安装sublime 2.安装package control 3.ctrl+shift+P输入install进入Install Packages 4.安装markdown preview 5.配置删 ...
- 【GoLang】golang 面向对象编程 & 面向接口编程
005.面向对象&接口编程 1 面向函数编程 1.1 将数据作为参数传递到函数入参 1.2 对象与函数是分离的 2 面向对象编程 2.1 使用者看起来函数作为对象的属性而非参数 2.2 函数属 ...
- C#操作txt文件
目的:txt文件的创建,读写操作 功能:创建一个winform窗体,当文件不存在时可以实现txt文件的创建 效果: 代码: 文件的创建(判断文件是否存在,不存在则创建新的文本文件): private ...
- winserver2008 Oracle 11g 安装
.在Windows Server2008R2上安装Oracle Database 11g Release 2,下载64位的安装程序,地址: 文件1:http://download.oracle.com ...
- ios UITextView 计算文字内容大小
先设置好 textView的内容文字,再调用以下代码,就能够得到文字内容的size,其中参数表示最大的size的尺寸,通常,高度应该不限制,宽度是控件的宽度. let newSize = statem ...
- WPF三大模板简介
WPF支持以下类型的模板: (1) 控件模板.控件模板可以将自定义模板应用到某一特定类型的所有控件,或是控件的某一实例.决定控件外观的是ControlTemplate,它决定了控件“长成什么样子”,因 ...