Java [Leetcode 102]Binary Tree Level Order Traversal

时间:2022-09-13 15:40:25

题目描述:

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]
]

解题思路:

运用广度优先搜索方法,同http://www.cnblogs.com/zihaowang/p/5149745.html类似。

代码如下:

/**
* 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>> result = new LinkedList<List<Integer>>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
if(root == null)
return result;
queue.offer(root);
while(!queue.isEmpty()){
int m = queue.size();
List<Integer> list = new LinkedList<Integer>();
for(int i = 0; i < m; i++){
if(queue.peek().left != null)
queue.offer(queue.peek().left);
if(queue.peek().right != null)
queue.offer(queue.peek().right);
list.add(queue.poll().val);
}
result.add(new LinkedList<Integer>(list));
}
return result;
}
}

  

Java [Leetcode 102]Binary Tree Level Order Traversal的更多相关文章

  1. &lbrack;LeetCode&rsqb; 102&period; Binary Tree Level Order Traversal 二叉树层序遍历

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

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

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

  4. leetcode 102&period; Binary Tree Level Order Traversal

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

  5. Java &lbrack;Leetcode 107&rsqb;Binary Tree Level Order Traversal II

    题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  6. Leetcode 102&period; 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&period; Binary Tree Level Order Traversal 二叉树的层次遍历 C&plus;&plus;

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

  8. leetcode 102 Binary Tree Level Order Traversal&lpar;DFS&vert;&vert;BFS&rpar;

    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 二叉树&plus;BFS

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

随机推荐

  1. Debian系列Linux&sol;Ubuntu 安装软件

    wps(http://community.wps.cn/download/) 优客天气(https://launchpad.net/indicator-china-weather/+download) ...

  2. 面试题2:BAT及各大互联网公司2014前端笔试面试题:HTML&sol;CSS篇

    BAT及各大互联网公司2014前端笔试面试题:HTML/CSS篇 Html篇: 1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? IE: trident内核 Firefox:gecko内 ...

  3. VPN各种常见状态码及修复方法

    1.633错误 :由于Windows系统本身的问题,在PPTP协议连接多次并断开之后,后导致一直出现633错误.参见微软的官方解决方案:http://support.microsoft.com/kb/ ...

  4. iOS:模态弹出窗控制器UIPopoverPresentationController

    模态弹出窗控制器:UIPopoverPresentationController 实质:就是将内容控制器包装成PopoverPresentationController的形式,然后再模态出来,必须指定 ...

  5. jQuery 源码基本框架

    抽丝剥茧, 7000+ 行的 jQuery 源码基本可以概括为以下的伪代码 (function (window, undefined) { //将 document 封装成 jQuery 对象并缓存 ...

  6. cocos2d-x lua 学习笔记&lpar;1&rpar; -- 环境结构

    Cocos2d-x 3.0超过环境的版本号来建立和前Cocos2d-x 2.0 差异较大的版本,从同时Cocos2d-x 3.0项目打包成apkAndroid的应用程序文件,步骤,构建环境有些乏味安德 ...

  7. RadioGroup&plus;TabHost

    =.= //MainActivity public class MainActivity extends TabActivity implements OnCheckedChangeListener ...

  8. Oracle空间查询 ORA-28595

    可使用数据库管理系统 (DBMS) 的结构化查询语言 (SQL).数据类型和表格式来处理地理数据库或安装了 ST_Geometry 类型的数据库中所存储的信息. 例如,在ArcMap中我们使用&quo ...

  9. JDK与Apache Tomcat服务器的安装步骤

    先解释一下JDK和Tomcat是什么: Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP ...

  10. BZOJ&lowbar;4196&lowbar;&lbrack;Noi2015&rsqb;软件包管理器&lowbar;树链剖分

    BZOJ_4196_[Noi2015]软件包管理器_树链剖分 题意: Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助 ...