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]
] 层序遍历 利用queue
 class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
if(root==null) return res;
queue.add(root);
while (!queue.isEmpty()) {
int levlnum = queue.size();
List<Integer> res_temp = new ArrayList<Integer>();
for (int i = 0;i<levlnum ;i++ ){ //把每层的左右节点都保存到queue里
//并讲当层的值从queue里弹出,加到res_temp 中
if(queue.peek().left!=null) queue.add(queue.peek().left);
if(queue.peek().right!=null) queue.add(queue.peek().right); res_temp.add(queue.poll().val);
}
res.add(res_temp);
}
return res;
}
}

102. Binary Tree Level Order Traversal ------层序遍历的更多相关文章

  1. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

  2. 102. Binary Tree Level Order Traversal 广度优先遍历

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

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

  4. [刷题] 102 Binary Tree Level Order Traversal

    要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...

  5. 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  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】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...

  8. 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, ...

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

随机推荐

  1. QT creator 编辑多个UI 文件 出现 无法解析的外部符号的错误

    创建一般的Qt Gui 程序一般会默认一个UI 文件 ,但是随着应用程序窗口的增多,同时编辑多个UI 界面是必须的. 假设我们已经创建好了一个QTUI的工程,里面已经默认了一个UI文件,但是想在添几个 ...

  2. Axure9 v9.0.0.3629 ~ v9.0.0.3633 授权密钥 【2019.02.05】

    现在提供一个支持v9.0.0.3629.v9.0.0.3630.v9.0.0.3631.v9.0.0.3632.v9.0.0.3633的授权码(后续的Beta更新版本应该能继续使用) 被授权人:zd4 ...

  3. windows 下安装securecrt 绿色版

  4. javascript基本语法和变量(转)

    转载来自 阮一峰老师的文章,地址为:http://javascript.ruanyifeng.com/grammar/basic.html#toc0 1.1语句 JavaScript 程序的执行单位是 ...

  5. SQL查询和编程基础

    本文转自http://www.cnblogs.com/Jolinson/p/3552786.html 这里的摘抄来自<Microsoft SQL Server 2008技术内幕:T-SQL语言基 ...

  6. 全局最小割模板(定S,不定T,找最小割)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  7. 170206、sping注解@autowired和@resource的区别

    新年第一天上班,新的一年,我们17加油!!! @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了 ...

  8. PAT 甲级 1003Emergency(Dijkstra最短路)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  9. C++ primer记录

    关于C++编程风格,可参考:Google 开源项目风格指南 第一章:开始 1. 头文件:由于嵌套包含文件的原因,一个头文件可能会被多次包含在一个源文件中.条件指示符可防止这种头文件的重复处理,例如:# ...

  10. delphi ----Raize(第三方控件) TRzNumericEdit

    一.Raize Edits 1.TRzNumericEdit IntegerOnly属性设置为false,可以输入小数. DisplayFormat := ',0.00;(,0.00)';;//小数默 ...