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

 
这一题和Binary tree level order traversal的区别在于,输出结果是从下往上一层层的遍历节点,有两种小聪明的方法:一、在上一题的最后反转res然后输出;二、使用栈。得到每一层的节点后,不直接压入res中,先存入栈中,然后利用栈先进后出的特点,再压入res,实现反转。这两题关键的部分在于如何单独得到每一层的节点。以下两种方法的主体部分也可以用上一题中的方法一,其核心思想不变
方法一:最后反转
 /**
* 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)
{
vector<vector<int>> res;
queue<TreeNode *> Q;
if(root) Q.push(root); while( !Q.empty())
{
int count=;
int levCount=Q.size();
vector<int> levNode; while(count<levCount)
{
TreeNode *curNode=Q.front();
Q.pop();
levNode.push_back(curNode->val);
if(curNode->left)
Q.push(curNode->left);
if(curNode->right)
Q.push(curNode->right);
count++;
}
res.push_back(levNode);
}
reverse(res.begin(),res.end()); //在上一题的基础上增加一行
return res;
}
};

方法二:利用栈

/**
* 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)
{
vector<vector<int>> res;
queue<TreeNode *> Q;
if(root) Q.push(root);
stack<vector<int>> stk;
while( !Q.empty())
{
int count=;
int levCount=Q.size();
vector<int> levNode; while(count<levCount)
{
TreeNode *curNode=Q.front();
Q.pop();
levNode.push_back(curNode->val);
if(curNode->left)
Q.push(curNode->left);
if(curNode->right)
Q.push(curNode->right);
count++;
}
stk.push(levNode); //压入栈
}
while(!stk.empty()) //出栈
{
res.push_back(stk.top());
stk.pop();
}
return res;
}
};

[Leetcode] Binary tree level order traversal ii二叉树层次遍历的更多相关文章

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

  2. [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历

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

  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. LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)

    题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...

  5. [leetcode]Binary Tree Level Order Traversal II @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...

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

  7. LeetCode - Binary Tree Level Order Traversal II

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

  8. Binary Tree Level Order Traversal(二叉树广度优先遍历或逐层遍历)

    来源:https://leetcode.com/problems/binary-tree-level-order-traversal Given a binary tree, return the l ...

  9. Binary Tree Level Order Traversal II(层序遍历2)

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

随机推荐

  1. Python 更换国内pip源

    pip国内的一些镜像: 阿里云 http://mirrors.aliyun.com/pypi/simple/   中国科技大学 https://pypi.mirrors.ustc.edu.cn/sim ...

  2. JavaScript基本概念(1)-声明提升

    声明提升: function > var > other var提升的时候,只是声明提升,但是赋值还是会在原来的位置. Javascript Hoisting:In javascript, ...

  3. 了解Python控制流语句——while 语句

    while 语句 Python 中 while 语句能够让你在条件为真的前提下重复执行某块语句. while 语句是 循环(Looping) 语句的一种.while 语句同样可以拥有 else 子句作 ...

  4. 【WXS全局对象】Global

    Global对象的方法调用时,无需使用 Global.parseInt(...),而是直接使用 parseInt(...) 方法: 名称 说明 parseInt(string, radix) 解析一个 ...

  5. lintcode 466. 链表节点计数

    466. 链表节点计数 计算链表中有多少个节点.   样例 给出 1->3->5, 返回 3. /** * Definition of ListNode * class ListNode ...

  6. [Clr via C#读书笔记]Cp2生成打包部署和管理应用程序和类型

    Cp2生成打包部署和管理应用程序和类型 部署问题 DLL Hell;安装的复杂性:安全性:代码访问安全性. csc.exe的简单使用. 元数据 定义表:引用表:清单表: 程序集 重用,版本控制,安全的 ...

  7. Kali渗透测试工具-nslookup

    1.交互模式 终端输入nslookup进入交互模式 (1)查询A地址记录(默认) set q=a A记录简单理解将域名转换成对应的IP地址 (2)查询mail exchanger set q=mx m ...

  8. Jquery复选框

    Jquery复选框 1.checkbox list选择 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  9. str和repr

    在Python2.6和Python3.0以及更早的版本中,在交互式模式下的输出本质上是使用repr,因此对于一些浮点数运算,会显示很多位: 4 / 5.0 #0.8000000000000004 但是 ...

  10. JS设置cookie,删除cookie(引)

    JS设置cookie,删除cookie(引) js设置cookie有很多种方法. 第一种:(这个是w3c官网的代码) <script> //设置cookie function setCoo ...