本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41964067

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

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
  / \
 2   3
    /
   4
    \
     5

The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

思路:

(1)题意为按层次从树顶到树根输出每层元素,该题和(Binary Tree Level Order Traversal)按层次从树根到树顶输出每层元素思路一样。

(2)只需要将Binary Tree Level Order Traversal得到的链表逆序即为本题答案。

详细过程请参照http://blog.csdn.net/pistolove/article/details/41929059

(3)希望对你有所帮助。谢谢。

算法代码实现如下所示:

public List<List<Integer>> levelOrderBottom(TreeNode root) {
	List<List<Integer>> result = new LinkedList<List<Integer>>();
	// 注意root为空时不能返回null
	if (root == null)
		return result;

	List<TreeNode> all = new LinkedList<TreeNode>();
	all.add(root);
	int first = 0; // 当前待访问节点,初始为第一个节点,即根节点
	int last = 1; // 当前链表中元素个数,初始只有一个
	while (first < all.size()) { // 如果待访问节点存在于链表
		last = all.size(); // 下一行访问开始,定位last为当前行最后一个节点下一个节点所在位置
		List<Integer> level = new LinkedList<Integer>();
		while (first < last) { // 如果first==last表示该行所有节点都被访问到了,跳出循环
			level.add(all.get(first).val);
			if (all.get(first).left != null) {
				all.add(all.get(first).left);
			}
			if (all.get(first).right != null) {
				all.add(all.get(first).right);
			}
			first++; // 每访问完一个节点就指向下一个节点
		}
		result.add(level);
	}

	//链表逆序    和按层次打印思路一样
	List<List<Integer>> fina = new LinkedList<List<Integer>>();
	for (int i = result.size() - 1; i >= 0; i--) {
		fina.add(result.get(i));
	}
	return fina;
}

Leetcode_107_Binary Tree Level Order Traversal II的更多相关文章

  1. 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...

  2. Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...

  3. LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...

  4. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

  5. 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)

    Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...

  6. 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

    [107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...

  7. LeetCode_107. Binary Tree Level Order Traversal II

    107. Binary Tree Level Order Traversal II Easy Given a binary tree, return the bottom-up level order ...

  8. 63. Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...

  9. [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

随机推荐

  1. PHP $_GET 变量

    $_GET 变量 预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值. 从带有 GET 方法的表单发送的信息,对任何人都是可见的(会显示在浏览器的地址栏 ...

  2. Jupyter Notebook 添加目录

    1.  安装 jupyter_contrib_nbextensions pip install jupyter_contrib_nbextensions 2. 配置 nbextension jupyt ...

  3. linux和android开发链接

    1.Tracy Mcgrady的专栏冰山一角:linux和Android底层开发,主要是mtk系列点击打开链接 2.郁闷Wednesday:嵌入式linux 单片机 android,点击打开链接 3. ...

  4. 给现有的word和pdf加水印

    iTextSharp简单生成pdf和操作pdf添加水印 给word加水印,利用的是aspose.words

  5. linux找不到动态链接库 .so文件的解决方法(转自:http://www.cnblogs.com/xudong-bupt/p/3698294.html)

    linux找不到动态链接库 .so文件的解决方法 如果使用自己手动生成的动态链接库.so文件,但是这个.so文件,没有加入库文件搜索路劲中,程序运行时可能会出现找不到动态链接库的情形. 可以通过ldd ...

  6. Web自动化框架LazyUI使用手册(6)--8种控件对应的class,及可对其进行的操作

    概述: 本文详述8种控件对应的class,及可对其进行的操作 回顾: 回顾一下,下文中的工具设计思路部分: http://blog.csdn.net/kaka1121/article/details/ ...

  7. 在Gazebo中使用DEM構建起伏地形環境

    所需資料下載地址: 1. https://bitbucket.org/osrf/gazebo_tutorials/raw/default/dem/files/ 数字高程模型(致謝谷歌翻譯)概述数字高程 ...

  8. Spark内存管理-UnifiedMemoryManager和StaticMemoryManager

    在Spark-1.6.0中,引入了一个新的参数spark.memory.userLegacyMode(默认值为false),表示不使用Spark-1.6.0之前的内存管理机制,而是使用1.6.0中引入 ...

  9. 在Activity,Service,Window中监听Home键和返回键的一些思考,如何把事件传递出来的做法!

    在Activity,Service,Window中监听Home键和返回键的一些思考,如何把事件传递出来的做法! 其实像按键的监听,我相信很多人都很熟练了,我肯定也不会说这些基础的东西,所以,前期,还是 ...

  10. Unable to access the IIS metabase.You do not have sufficient privilege

    今天在用vs打开以前老代码的时候报如下问题,无法打开工程了,从提示来不大可能是因为vs的版本引起的,本身我用的是最新版的vs. 网上查了下解决方法如下:找到你电脑中的如下路径"C:\Wind ...