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 7

return 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 层序遍历二叉树的更多相关文章

  1. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

  2. 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 ...

  3. 102. Binary Tree Level Order Traversal ------层序遍历

      Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to righ ...

  4. [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 ...

  5. 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 ...

  6. [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, ...

  7. LeetCode(32)-Binary Tree Level Order Traversal

    题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...

  8. (二叉树 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 ...

  9. 【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, ...

随机推荐

  1. Android学习笔记(一)——新建一个项目

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 1.打开Android Studio时出现以下界面,点击”start a new Android Studio ...

  2. 关闭MyEclipse Derby服务

    MyEclipse的Servers视图出现 MyEclipse Derby服务,一直想把它去掉在网上搜索了下,现已解决. 如下,MyEclipse菜单:window-->Preferences- ...

  3. Caffe学习系列(9):solver优化方法

    介绍了各种优化算法 参考:http://www.cnblogs.com/denny402/p/5074212.html

  4. PHP中函数sprintf .vsprintf (占位符)

    sprintf()格式化字符串写入一个变量中. vsprintf()格式化字符串些写入变量中. <?php $num1 = 123; $num2 = 456; $txt = vsprintf(& ...

  5. 4 Handler相关类——Live555源码阅读(一)基本组件类

    这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. Handler相关类概述 处理程序相关类一共有三个,其没有派生继承关系,但是其有友元关系和使用关系 ...

  6. python entrypoint

    entrypoint, 实际是一张匹配表.匹配简短指令和具体的python函数的执行路径.有点快捷方式的概念. 不同的是,这种快捷方式不仅可以给命令行使用,还可以供其他代码简单调用,而无需关注太多细节 ...

  7. C#的is和as操作符来进行强制类型转换&&值类型的拆箱、装箱

    if(o is Employee) { Employee e=(Employee)o; //在if语句剩余的部分中使用e; } Employee e=o as Employee; if(e!=null ...

  8. Struts2学习笔记《二》

    struts.xml配置文件的全部配置元素:

  9. spring + myBatis 常见错误:@Autowired注解失败

    今天配置spring+myBatis的时候,使用注解@Autowired把持久层dao注入service层的时候总是报错. 查了好久才发现,居然是配置文件路径写错了.basepackge的路径一定要正 ...

  10. C++实现VPN工具之代码示例

    创建.连接.挂断.删除VPN实现起来并不难,下面给出一套比较完整的代码.该段代码只是示例代码,但是已经通过了编译,对API的使用和VPN操作步骤是没问题的.具体每个API代表的意义可以参看<C+ ...