【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, ...
随机推荐
- MyBatis 一级、二级缓存
一级 默认session就有一级缓存,session有C/U/D操作或者session.clearCache();session.commit();都会清空缓存: 二级在mapper.xml中添加&l ...
- Bootstrap入门(二十二)组件16:列表组
Bootstrap入门(二十二)组件16:列表组 列表组是灵活又强大的组件,不仅能用于显示一组简单的元素,还能用于复杂的定制的内容. 1.默认样式列表组 2.加入徽章 3.链接 4.禁用的列表组 5. ...
- Linux笔记(七) - 网络命令
(1)给用户发信息:write例:write admin(ctrl+d结束)(2)发广播信息:wall例:wall hello world!(3)测试网络连通性:ping-c 发送次数例:ping - ...
- 我的2016年终总结(PF项目框架设计心得分享 2.0rc)
在无数的日夜里,熬出了多少的黑眼圈,致勤勤恳恳工作的各位朋友与自己.每到了年末的时候总想写的什么,主要是为了回顾以往一年里到底做了什么,这便是年终总结的主要意义.在此我将要总结的是和我在技术层面上成长 ...
- 【The Most Important】浅谈JSP表单Post方式中文乱码问题
首先祝大家鸡年吉祥!在这里我要说下这两天里这个问题困扰着我,大过年的心情都被烦扰的不好了,所以我带着兴奋的心情来赶快完成这篇博客,解决大家的问题.我的问题是post方式传递表单数据,Tomcat服务器 ...
- We Chall-Training: Encodings I -Writeup
MarkdownPad Document html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,ab ...
- for语句输出图形
一.输出以下图形 ******************************** 用for...for...嵌套循环,内循环控制每一行的个数(即列数),外循环控制行数 class ForDraw { ...
- 【小分享】Date对象封装,时间格式化函数time()
今天再来分享下Date的应用知识点 先看效果,类似于php里边的time('yyyy-mm-dd')用法,但是我这里没有完全依照php的参数格式来,如果有需要稍微修改一下就行. 首先,明确需要用到的参 ...
- 初识Jenkins
近期,接手了一个活,我要搭一个Jenkins持续集成的平台,所以,就把这次工作的收获分享给大家了. Jenkins是什么 Jenkins插件配置 Jenkins怎么用 新建job 系统配置 添加用户 ...
- matlab 利用while循环计算平均值和方差(第二版)
第一版中因为公式中含有:分母项:n(n-1),而程序并没有对输入数进行判定,如果仅仅输入一个或者一个都不输入,将会出现除0的情况 基于此,进行第二版改进. 代码: % 脚本文件:states.m % ...