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, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
解题思路:
运用广度优先搜索方法,运用队列的方法,每次遍历一层的叶节点,并把下一层的叶节点加入到队列中。每次遍历一层节点结束时候,将该层节点组成的list放到整体的list之前,实现由底到高的排列。
代码如下:
/**
* 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>> levelOrderBottom(TreeNode root) {
List<List<Integer>> list = new LinkedList<List<Integer>>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
if(root == null)
return list;
queue.offer(root);
while(!queue.isEmpty()){
int num = queue.size();
List<Integer> levelList = new LinkedList<Integer>();
for(int i = 0; i < num; i++){
if(queue.peek().left != null)
queue.offer(queue.peek().left);
if(queue.peek().right != null)
queue.offer(queue.peek().right);
levelList.add(queue.poll().val);
}
list.add(0, levelList);
}
return list;
}
}
Java [Leetcode 107]Binary Tree Level Order Traversal II的更多相关文章
- 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 ...
- (二叉树 BFS) 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 ----- java
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 (二叉树阶层顺序遍历之二)
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
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 二叉树的层次遍历 II
相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
- Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS
题意是倒过来层次遍历二叉树 下面我介绍下BFS的基本框架,所有的BFS都是这样写的 struct Nodetype { int d;//层数即遍历深度 KeyType m;//相应的节点值 } que ...
随机推荐
- 导入 github 步骤
https://github.com/dotnet/corefx 如果出现未能找到解决方案的情况,则找项目文件打开,如:
- error: The shader requires a sampler in slot 0 which hasn't been set [XXXsampler]
About the sampler, you need to assign it to your pixelshader. m_d3dContext.Get()->PSSetSamplers(0 ...
- [C/CPP系列知识] 在C中使用没有声明的函数时将发生什么 What happens when a function is called before its declaration in C
http://www.geeksforgeeks.org/g-fact-95/ 1 在C语言中,如果函数在声明之前被调用,那么编译器假设函数的返回值的类型为INT型, 所以下面的code将无法通过编译 ...
- 【log4net】配置文件
相关资料: http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html 注意: //如果为了使得应用程序的配置文件(web/app.con ...
- PHP中$_SERVER获取当前页面的完整URL地址
PHP中$_SERVER获取当前页面的完整URL地址,其实很简单,主要是通过$_SERVER超全局变量来实现的. 具体PHP中$_SERVER获取当前页面的完整URL地址如下. #测试网址: ...
- POJ 1026 Cipher(置换群)
题目链接 题意 :由n个数字组成的密钥,每个数字都不相同,都在1-n之间,有一份长度小于等于n的信息,要求将信息放到密钥下边,一一对应,信息不足n的时候补空格,然后将位置重新排列,将此过程重复k次,求 ...
- SDUT2141数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2141&cid=1186 #include<cstdio> #include& ...
- .net web部署(IIS Express && Nancy Self-Hosting)
http://d.hatena.ne.jp/fkmt5/20140330/1396195246 [1]Nancy Web配置注意事项 添加url:netsh http add urlacl url=h ...
- 深入理解JVM—Java 6 JVM参数配置说明
原文地址:http://yhjhappy234.blog.163.com/blog/static/316328322011119111014657/ 使用说明< xmlnamespace pre ...
- hdu1875
http://acm.hdu.edu.cn/showproblem.php?pid=1875 2 2 10 10 20 20 3 1 1 2 2 1000 1000 给定坐标 //最小生成树 #inc ...