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). (Easy)

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

[
[15,7],
[9,20],
[3]
]

分析:

只需要在层序遍历的基础上(参见Binary Tree Level Order Traversal)将最后结果vector reverse即可

代码:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
vector<vector<int>> result;
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
if (root == nullptr) {
return result;
}
queue<TreeNode* > que;
que.push(root);
while (!que.empty()) {
int sz = que.size();
vector<int> temp;
for (int i = ; i < sz; ++i) {
TreeNode* cur = que.front();
que.pop();
temp.push_back(cur -> val);
if (cur -> left != nullptr) {
que.push(cur -> left);
}
if (cur -> right != nullptr) {
que.push(cur -> right);
}
}
result.push_back(temp);
}
reverse(result.begin(), result.end());
return result;
}
};

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

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

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

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

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

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

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

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

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

  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. 【python之路30】反射

    一.反射 1.反射的基本介绍: 反射是所有程序的专有名词,在java,C#语言中都存在反射,那么什么是反射呢? python中 的反射概括来说:是通过字符串的形式导入模块,并通过字符串的形式去模块中寻 ...

  2. TZ_05_Spring_事物的xml开发和annotation开发

    1.Spring_事物的xml开发 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...

  3. ubuntu中vi下删除键和上下左右键输入字符异常(ABCD)

    刚安装的Ubuntu系统,使用vi编辑文本的时候, 出现以下现象: 点删除键输入了 D 回车无效 上下左右为字母 光标乱跳 原因: 自带的vi功能问题 解决: 卸载原有vi,重新安装完整版本vim 执 ...

  4. git学习记录——基础概念和文件的基本操作

    夸一下git git是当前世界上最先进的分布式版本控制系统 优势: 1.不必联网 2.Git极其强大的分支管理,把SVN等远远抛在了后面. 集中式的代表CVS和SVN 分布式的代表BitKeeper, ...

  5. mysql5 msi安装版

    有安装版为啥要用解压版? 搞不懂为啥大佬们都喜欢解压版? http://ftp.ntu.edu.tw/MySQL/Downloads/MySQLInstaller/mysql-installer-co ...

  6. CSS 教程 - 闭合浮动元素

    按照CSS规范,浮动元素(floats)会被移出文档流,不会影响到块状盒子的布局而只会影响内联盒子(通常是文本)的排列. 因此当其高度超出包含容器时,一般父容器不会自动伸长以闭合浮动元素. 但是有时我 ...

  7. 2019.7.27 NOIP模拟测试9 反思总结

    先来整理题目 T1题目大意:给出n个数字和一个质数作为模数,一个变量x初始值为1.进行m次操作,每次让x随机乘上n个数中的一个,问m次操作以后x的期望值. 答案一定可以用分数表示,输出分子乘分母逆元的 ...

  8. java-String-StringBuffer

    一 String 1.1 == 和 equal() System.out.println("-------两个内容相同,创建方式不同的字符串,面试题--------"); Stri ...

  9. CSS实现火焰效果

    代码如下 //主要就是用css动画实现的 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  10. Lavavel 程序报错 MassAssignmentException in Model.php line 452: _token

    Lavarel 用类似于下面命令插入数据时候出错 Comment::create($request->all()) 错误页面截图如下: 错误原因:下面这行代码应该写到对应的Model里面,而不是 ...