【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, ...
随机推荐
- --@ui-router--登录页通过路由跳转到内页的demo
今天还是来说一下angular中的路由模块.我们实际项目中,各个页面的切换是经常会与Auth相关的.比如我网站的后台,是需要登录过的用户才能进去,那么我们用angularJS做前端路由的时候应该怎么完 ...
- oc是一个全动态语言,oc的一切都是基于runtime实现的!
oc是一个全动态语言,oc的一切都是基于runtime实现的! 从以下三方面来理解runtime吧! 1. 传统的面向过程的语言开发,例如c语言.实现c语言编译器很简单,只要按照语法规则实现一个LAL ...
- OGG数据仓库以及单向复制(一)
Item Source System Target System Platform OEL5.4 OEL5.4 Hostname gc1 oraclelinux54.cuug.net Database ...
- SVG的内部事件添加
SVG的内部事件添加: <%@ page language="java" contentType="text/html; charset=UTF-8" p ...
- 在Express中安装XTemplate
上一节讲了安装Express,并且生成了一个"microblog"的工程,我们的目标是在工程下安装XTemplate: 1.安装xtpl npm install xtpl xtem ...
- 你真的懂ajax吗?
前言 总括: 本文讲解了ajax的历史,工作原理以及优缺点,对XMLHttpRequest对象进行了详细的讲解,并使用原生js实现了一个ajax对象以方便日常开始使用. damonare的ajax库: ...
- POJ2407(欧拉函数)
Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13598 Accepted: 6771 Descri ...
- .Net学习难点讨论系列17 - 线程本地变量的使用
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 如何一步一步用DDD设计一个电商网站(十三)—— 领域事件扩展
阅读目录 前言 回顾 本地的一致性 领域事件发布出现异常 订阅者处理出现异常 结语 一.前言 上篇中我们初步运用了领域事件,其中还有一些问题我们没有解决,所以实现是不健壮的,下面先来回顾一下. 二.回 ...
- 蓝桥网试题 java 基础练习 数列特征
----------------------------------- Collections.sort(list);是个好东西 但是要学会排列 然后你才能浪 -------------------- ...