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. FAT,FAT32,NTFS单目录文件数量限制

    http://hi.baidu.com/huaxinchang/item/5ba53ba9b29631756dd4551b —————————————————————————————————————— ...

  2. C++ Scoket的升级版(多态的运用)

    //Socket报文发送c++升级版 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #in ...

  3. 第10步:DBCA创建实例

    注意,创建磁盘组时需要以oracle用户身份执行,在那之前可能需要以root身份执行xhost+,即命令: 代码1 [root@sgdb1~]# xhost+ [root@sgdb1~]# su - ...

  4. uva 10494 - If We Were a Child Again 大数除法和取余

    uva 10494 - If We Were a Child Again If We Were a Child Again Input: standard inputOutput: standard ...

  5. structure machine learning projects 课程笔记

    orthogonalization/ one metric train.dev/test 划分 开发集和测试集一定来自同一分布  onthe  same distribution Human leve ...

  6. VC++ Debug格式化数值显示

    When you watch variables in the Watch or Quick Watch window, the values are displayed using the defa ...

  7. Android 触摸及手势操作GestureDetector

    现在的智能手机不敢说百分百的都是触摸屏,也应该是百分之九九以上为触摸屏了,触摸屏为我们操作无键盘.无鼠标的手机系统带来了很多的便利.当用户触摸屏幕时会产生很多的触摸事件,down.up.move等等. ...

  8. SDN开发过程中遇到的一些问题总结

    我用的是ryu控制器,用Mininet作为网络系统平台. 当启动控制器的时候如果提示:unsupported version 0x1. if possible, set the switch to u ...

  9. 【BZOJ1822】[JSOI2010]Frozen Nova 冷冻波 几何+二分+网络流

    [BZOJ1822][JSOI2010]Frozen Nova 冷冻波 Description WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀 ...

  10. 【BZOJ3673/3674】可持久化并查集/可持久化并查集加强版 可持久化线段树

    [BZOJ3674]可持久化并查集加强版 Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了! ...