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

code: 1A 36ms
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > levelOrderBottom(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<vector<int> > res;
if(root == NULL)
return res;
queue<TreeNode *> qu;
qu.push(root);
qu.push(NULL);
vector<int> onelevel;
while(true)
{
TreeNode *cur = qu.front();
qu.pop();
if(cur == NULL)
{
res.push_back(onelevel);
onelevel.clear();
if(qu.empty())
break;
qu.push(NULL);
}
else
{
onelevel.push_back(cur->val);
if(cur->left)
qu.push(cur->left);
if(cur->right)
qu.push(cur->right);
} }
reverse(res.begin(),res.end());
return res;
}
};

【LeetCode】Binary Tree Level Order Traversal II的更多相关文章

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

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

  3. 【题解】【BT】【Leetcode】Binary Tree Level Order Traversal

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

  4. 【Leetcode】Binary Tree Level Order Traversal

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

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

  6. 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)

    这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...

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

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

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

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

随机推荐

  1. C#基础(二)——C#中的构造函数

    构造函数主要是用来创建对象时为对象赋初值来初始化对象.总与new运算符一起使用在创建对象的语句中 .A a=new A(); 构造函数具有和类一样的名称:但它是一个函数具有函数的所有特性,同一个类里面 ...

  2. HTML 表单和表格

    1.使用表单标签 网站使用 HTML 表单可与用户进行交互,表单元素是允许用户在表单中输入内容,比如:文本框.文本域.单选框.复选框.下拉列表.按钮等等,表单可以把浏览者输入的数据传送到服务器端,这样 ...

  3. PHP 面向对象之自定义类

    所谓面向对象就是什么时候什么东西做什么,我们设计类的时候需要想的就是怎么做的内容,那么怎么样的一个类才算是符合OOP的思想呢,答案是:这个类写好之后,在使用的过程中,能准确的代表一个事物,在书写的时候 ...

  4. php设计模式:工厂模式

    php设计模式:工厂模式 意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类. 工厂模式实现: 工厂模式中任何创建对象的工厂类都要实现这个接口,实现接口的方法体中都要实现接口中的方法,它声明 ...

  5. Omnithreadlibary学习(3)-IOmniTask异步执行SendMessage

    在任务中发送消息, 可以是函数或者对象方法 TOmniTaskMessageEvent = procedure(const task: IOmniTaskControl; const msg: TOm ...

  6. 快速生成json实体类

    读取一个json文件,并与实体相对应: static void Main(string[] args) { string json = ""; FileStream fs = ne ...

  7. POJ 2442 Sequence 优先队列

    题目: http://poj.org/problem?id=2442 #include <stdio.h> #include <string.h> #include <q ...

  8. IAR ARM、IAR STM8、IAR MSP430共用一个IDE

    转自IAR ARM.IAR STM8.IAR MSP430共用一个IDE 试了安装好多个不同版本不同编译器的IAR,终于明白不同编译器的IAR共用IDE的条件,把几个不同编译器的IAR安装在一起,共用 ...

  9. java api如何获取kafka所有Topic列表,并放置为一个list

    kafka内部所有的实现都是通过TopicCommand的main方法,通过java代码调用API,TopicCommand.main(options)的方式只能打印到控制台,不能转换到一个list. ...

  10. Android开源项目发现---GridView 篇(持续更新)

    1. StaggeredGridView 允许非对齐行的GridView 类似Pinterest的瀑布流,并且跟ListView一样自带View缓存,继承自ViewGroup 项目地址:https:/ ...