题目描述:

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的更多相关文章

  1. 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 ...

  2. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

  3. [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 ...

  4. (二叉树 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. leetcode 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II

    相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

  9. Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS

    题意是倒过来层次遍历二叉树 下面我介绍下BFS的基本框架,所有的BFS都是这样写的 struct Nodetype { int d;//层数即遍历深度 KeyType m;//相应的节点值 } que ...

随机推荐

  1. uva 10304

    最优二叉查找数 看了这位大牛 的博客 http://www.cnblogs.com/lpshou/archive/2012/04/26/2470914.html /****************** ...

  2. Javascript中的Cookie操作

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. android Notification定义与应用

    首先要明白一个概念: Intent 与 PendingIntent 的区别: Intent:是意图,即告诉系统我要干什么,然后做Intent应该做的事,而intent是消息的内容 PendingInt ...

  4. 天灵灵,地灵灵,但愿这个一定灵!!!python调用win32api,启动应用程序窗口

    这个是逼到没办法,C#那一套,一点基本没有. 还好,网上找到例程,可以指定帐户启动进程,但愿可以摆脱WIN SERVICE启动产生的SESSION 0 隔离问题. 因为这个问题,以SERVICE启动的 ...

  5. hdu 4447 Yuanfang, What Do You Think?

    思路: 这题有个结论也可以自己归纳: 对于给定的n,其约数用pi表示 T(n)=T(p1)T(p2)……T(pn)T(n') 其中T(n')是这个式子所独有的也就是 T(n')=(x^n-1)/T(p ...

  6. CF 317D Game with Powers

    题解: 将一个数的指数看着一个堆,问题变成这些堆的异或值 分析一个堆的情况,打SG表. #include<stdio.h> #include<string.h> ]; char ...

  7. JDK安装配置问题

    JDK安装过程中会有两个安装提示,一个是jdk的安装,一个是jre的安装

  8. SqlDataAdapter用法

    SqlDataAdapter和SqlCommand区别: SqlCommand就是是命令了,可以用它来执行SQL命令: SqlDataAdapter就是数据适配器了,它是用于在数据源和数据集之间通讯的 ...

  9. Xamarin.Android 入门之:Android API版本设置

    一.引言 Xamarin.Android有几个Android API级别设置,确定多个版本的Android应用程序的兼容性.本博客解释了这些设置意味着什么,如何配置它们,以及它们在运行时对您的应用程序 ...

  10. Go推出的主要目的之一就是G内部大东西太多了,系统级开发巨型项目非常痛苦,Go定位取代C++,Go以简单取胜(KISS)

    以前为了做compiler,研读+实现了几乎所有种类的语言.现在看语法手册几乎很快就可以理解整个语言的内容.后来我对比了一下go和rust,发现go的类型系统简直就是拼凑的.这会导致跟C语言一样,需要 ...