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,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

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

--------------------------------------------------------------------------------

这个就是二叉树的层序遍历,和leetcode102. Binary Tree Level Order Traversal类似,只是要把最后得到的二维数组逆转一下就成。

C++代码:

/**
* 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 {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
if(!root) return {};
queue<TreeNode*> q;
q.push(root);
vector<vector<int> > vec;
while(!q.empty()){
vector<int> v;;
for(int i = q.size(); i > ; i--){
auto t = q.front();
q.pop();
v.push_back(t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
vec.push_back(v);
}
reverse(vec.begin(),vec.end());
return vec;
}
};

(二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II的更多相关文章

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

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

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

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

  4. 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. Java [Leetcode 107]Binary Tree Level Order Traversal II

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

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

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

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

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

随机推荐

  1. webpack 单独打包指定JS文件(CopyWebpackPlugin)

    背景: 不确定打出的前端包所访问的后端IP,需要对项目中IP配置文件单独拿出来,方便运维部署的时候对IP做修改.因此,需要用webpack单独打包指定文件.npm install --save-dev ...

  2. 服务器端Session和客户端Session

    客户端Session和服务器端Session 当用户首次与web服务器建立连接的时候,服务器会给用户分发一个SessionID作为标识.SessionID是一个由24个字符组成的随机字符串.用户每次提 ...

  3. springCloud feign使用/优化总结

    基于springCloud Dalston.SR3版本 1.当接口参数是多个的时候 需要指定@RequestParam 中的value来明确一下. /** * 用户互扫 * @param uid 被扫 ...

  4. Visual Studio插件开发基础

    Visual Studio插件主要有两种:Add-in 和 VSX(Visual Studio eXtensibility) 两者区别可参考这篇文章:Visual Studio Extensions ...

  5. MongoDB 中数据的替换方法实现 --类Replace()函数功能

    关键字: MongoDB,Replace,forEach 近日接到一个开发需求,因业务调整,需要DBA协助,将MongoDB数据库中某集合的进行替换.例如我们需要将集合A中B字段中,有关<美好& ...

  6. MongoDB自学(3)

    MongoDB关系:MongoDB的关系表示多个文档之间在逻辑上的相互联系.文档之间可以通过嵌入和引用来建立联系.关系:1:11:NM:1M:N 嵌入式:{ id:11;name:嘻嘻嘻;addres ...

  7. python之list和tuple

    https://www.cnblogs.com/evablogs/p/6691743.html list和tuple区别: 相同:均为有序集合 异同:list可变,tuple一旦初始化则不可变 lis ...

  8. js得到规范的时间格式函数,并调用

    1.js得到规范的时间格式函数 Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth()+1 ...

  9. idea 右键无java class选项

    项目中新建module之后,要在该目录下新增java Class文件,右键——>New发现无Java Class选项. File –Project Structure或者ctrl+alt+shi ...

  10. 【续】5年后,我们为什么要从 Entity Framework 转到 Dapper 工具?

    前言 上一篇文章收获了 140 多条评论,这是我们始料未及的. 向来有争议的话题都是公说公的理,婆说婆的理,Entity Framework的爱好者对此可以说是嗤之以鼻,不屑一顾,而Dapper爱好者 ...