【LeetCode】Binary Tree Level Order Traversal 【BFS】
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】的更多相关文章
- 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; * ...
- 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 ...
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- [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 ...
- 【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 ...
- 【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, ...
- LeetCode(32)-Binary Tree Level Order Traversal
题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...
- [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[149]Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- Scrum
Scrum[编辑] 维基百科,自由的百科全书 Scrum是一种敏捷软件开发的方法学,用于迭代式增量软件开发过程.Scrum在英语是橄榄球运动中争球的意思. 虽然Scrum是为管理软件开发项目而开发 ...
- LCA 倍增
最近公共祖先 对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. #include<cstdio> #include&l ...
- 不使用模板导出Excel(C#版本)
不多说,直接上干货! using System; using System.Collections.Generic; using System.Linq; using System.Web; usin ...
- storm遇到问题汇总
http://www.reader8.cn/jiaocheng/20131023/2139887.html 错误1:在windows下运行ExclamationTopology或者WordCountT ...
- Javascript 继承 图形化展示
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" conte ...
- CSS实现的几款不错的菜单栏
前言 自从做了智慧城市这个项目之后,我一个做后端的开发者,瞬间转为前端开发,不过我还是很喜欢前端的.前端那些事,其实蛮有意思的,HTML实现的是静态的,使用ajax之后就可以和数据库交互了,加上js和 ...
- JavaScript易混淆知识点小回顾--数组方法与字符串方法;
数组属性: arr.length;查看数组的长度 arr.Pop;删除数组最后一个元素; 数组的方法: arr.push();添加到数组末端; arr.shift();删除数组的第一个元素; arr. ...
- TFS实现需求工作项自动级联保存
目前在一个大型的金融客户软件研发平台项目实施和支持过程中,客户的质量管理团队基于该平台以及结合其它的平台数据,针对需求管理和业务过程需要拟定了一套完整的需求提出.评审.设计以及实现的流程.基于这套流程 ...
- VS2015中VB.NET类(dLL)里下载并读取文件
最近要从一个http上下载个文件,差点就直接telnet了,突然发现了这个: My.Computer.Network.DownloadFile("目标文件网址") 但是还得读取它, ...