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, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
给出一棵二叉树,返回其节点值的层次遍历,逐层从左往右访问
使用队列存储每一层的节点,因为队列是先进先出的。进入循环弹出这一层节点存入数组,把弹出的节点的左右子节点存入队列中,依次循环。
需要注意的几点:
(1)队列中使用offer方法添加元素,如果发现队列已满无法添加元素的话,会直接返回false。
如果用add方法添加元素,若超出了队列长度会直接抛异常。
(2)队列中使用poll取出并移除头元素,如果队列为空,返回null。如果食用remove,若队列为空,抛出NoSuchElementException异常。
(3)判断队列是否为空queue.isEmpty( ).
(4)新建队列 Queue<TreeNode> queue = new LinkedList<TreeNode>( );
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
ArrayList result = new ArrayList();
if (root == null) {
return result;
} Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
ArrayList<Integer> level = new ArrayList<Integer>();
int size = queue.size();
for(int i = 0; i < size; i++) {
TreeNode node = queue.poll();
level.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
result.add(level);
}
return result;
}
}
leetcode 102. Binary Tree Level Order Traversal的更多相关文章
- [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 102 Binary Tree Level Order Traversal ----- java
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Java [Leetcode 102]Binary Tree Level Order Traversal
题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- 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 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Java for 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 102 Binary Tree Level Order Traversal(DFS||BFS)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS
二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
- leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历
基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行 ...
随机推荐
- 表x有 一列 ,程序每次生成id的时候都先从这里获取最大值再加1,初始值是A0001,然后到A9999的时候则是到B0001 共5位
drop table x gocreate table x(id varchar(10))--insert into x values('A001')gowith a as (select ISNUL ...
- php返回json数据中文显示的问题
PHP5.4版本,已经给Json新增了一个选项: JSON_UNESCAPED_UNICODE.加上这个选项后,就不会自动把中文编码了. echo json_encode("厦门" ...
- Java数据库——事务处理
在数据库中执行5条SQL语句,这些SQL语句本身需要保持一致,即要么同时成功,要么同时失败 事务基本操作 //============================================= ...
- Java关键字——super
使用super关键字可以从子类中调用父类中的构造方法.普通方法和属性 与this调用构造方法的要求一样,语句必须放在子类构造方法的首行 this和super都可以调用构造方法,但是两者不能同时出现,调 ...
- How to read a scientific paper
How to read a scientific paper Nothing makes you feel stupid quite like reading a scientific journal ...
- php生成excle
方法一: 新建index.php,代码如下 <?php header("Content-type:application/vnd.ms-excel"); header(&qu ...
- Elmah 日志记录组件
http://www.cnblogs.com/jys509/p/4571298.html 简介 ELMAH(Error Logging Modules and Handlers)错误日志记录模块和处理 ...
- 轻取帝国CMS管理员密码
“帝国”CMS是一套著名的PHP整站程序,是国内使用人数最多的PHPCMS程序之一.令人无奈的是,“帝国”虽然把势力壮大了,却忽略了自身防护的建设,结果在黑客攻击下,“帝国”沦陷了.“帝国”CMS曝出 ...
- TFS2012 安装 配置笔记
TFS2012安装 具体请看文档.. http://yunpan.cn/cmt4X6S7TjEgq 访问密码 464e TFS2012配置 环境:VS2012 SQL2008 T ...
- webuploader横向按钮样式
#picker{display: inline-block;line-height: 1.428571429;vertical-align: middle;margin: 0 12px 0 0;wid ...