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]
] 广度优先搜索,一般做法都是采取一个队列来做。
不过这题有个比较不一样的是他返回的格式是List<List>>的格式,所以需要做一些处理。
直接上代码了,很简单的题,

JAVA CODE:
/**
* 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 ) { List<List<Integer>> returnList = new ArrayList<List<Integer>>(); if(root == null){
return returnList;
} List<Integer> temp = null; Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.add( root ); while( !queue.isEmpty() ) {
Queue<TreeNode> tempQ = new LinkedList<TreeNode>();
temp = new ArrayList<Integer>();
while( !queue.isEmpty() ) {
TreeNode tn = queue.poll(); if( tn.left != null ) {
tempQ.add( tn.left );
}
if( tn.right != null ) {
tempQ.add( tn.right );
}
temp.add( tn.val );
} queue = tempQ;
returnList.add( temp );
} return returnList;
}
}

【LeetCode】Binary Tree Level Order Traversal 【BFS】的更多相关文章

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

  2. Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS

    二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

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

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

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

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

  6. 【Leetcode】【Easy】Binary Tree Level Order Traversal II

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

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

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

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

  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. andorid 开放工具集合

    1.开放工具集合 http://www.androiddevtools.cn/

  2. jquery serialize 和 console 漫谈

    serialize   获取表单中所有的数据 今天刚刚发现这个神奇的东西,顿时感觉高大上了很多,比以前一个一个用 val()取出来值  ,然后再 ajax 方便多了 代码示例 $(document). ...

  3. iPhone 屏幕分辨率

    5S     640 x 1136 5       640 x 1136 4s     640 x 960 4       640 x 960 3gs   320 x 480

  4. thinkPHP 模板中的语法

    一.导入CSS和JS文件   1.css link       js  scr        <link rel='stylesheet' type='text/css' href='__PUB ...

  5. bmp图片显示

    文件IO项目: 在开发板屏幕上循环显示目录里的图片 a.按照一定的间隔循环显示目录里的bmp图片 b.实现手指滑动来显示目录里的图片(bmp,jpg)上一张,下一张 d1: 1.能操控屏幕(查询开发板 ...

  6. 强行在MFC窗体中渲染Cocos2d-x 3.6

    [前言] 把Cocos2dx渲染到另一个应用程序框架中的方法,在2.x时代有很多大神已经实现了,而3.x的做法网上几乎找不着.这两天抽空强行折腾了一下,不敢独享,贴出来供大家参考. [已知存在的问题] ...

  7. java中Integer比较需要注意的问题

    java中Integer比较需要注意的问题 package com.srie.test; import java.util.HashMap; import java.util.Map; public ...

  8. windows环境下使用git客户端、github和tortoisegit管理项目代码

    一.为什么 为什么不用svn? svn是一个优秀的代码和版本管理工具,使用svn只需要搭建好svn中央仓库,配置本地svn客户端即可,自从google code关闭服务之后,互联网上已经没有非常好的公 ...

  9. (一)Redis在windows下的安装和使用

    1.下载redis服务端,地址:https://github.com/MSOpenTech/redis/releases 包含安装程序和源码. 2.解压<Redis-x64-3.2.100.zi ...

  10. spring-mvc.xml配置

    1.自动扫描 <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component-scan base-pac ...