Java [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, 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的更多相关文章
- [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, ...
- 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, ...
- 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, ...
- 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, ...
- Java [Leetcode 107]Binary Tree Level Order Traversal II
题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- 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, ...
- 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, ...
- 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, ...
- Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS
二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
随机推荐
- 【BZOJ】【3093】【FDU校赛2012】A Famous Game
概率论 神题不会捉啊……挖个坑先 orz 贾教 & QuarterGeek /********************************************************* ...
- 【BZOJ】【1013】【JSOI2008】球形空间产生器sphere
高斯消元 高斯消元模板题 /************************************************************** Problem: 1013 User: Tun ...
- Matlab计算两集合间的海明距离
一.问题描述 B1[1 2 3 4 5 6 7 8 9] B2[12 13 14 21 31 41 51 1 1 81 1 1] 两个十进制矩阵,行数不一样,分别是n1和n2,列数必须一致,为nwo ...
- SqlBulkCopy 简单运用
using(SqlConnection conn = new SqlConnection(str)) { conn.Open(); using (System.Data.SqlClient.SqlBu ...
- cf 363D
贪心加二分 虽然比赛后才过 ........ /************************************************************************* &g ...
- 如果使用得当,MySQL 也可以化身 NoSQL
[编者按]随着互联网和移动互联网的发展,各个机构都需要支撑远超过以往的数据.而在这个需求的刺激下,IT 领域出现了大量数据处理技术,其中之一就是 NoSQL .灵活的数据类型,高效的处理能力,让 No ...
- IOS 中的MVC设计模式
- IOS时间格式转换
在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理. 例如:如何将格式为“12-May-14 05.08.02.000000 PM” ...
- 【Linux高频命令专题(15)】more
more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...
- Python list去重及找出,统计重复项
http://bbs.chinaunix.net/thread-1680208-1-1.html 如何找出 python list 中有重复的项 http://www.cnblogs.com/feis ...