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]

]

这个题目要采用广度优先遍历,所以我们需要一个队列,为了记录节点的深度我用了一个map映射来记录节点的深度

当vec的长度小于节点的深度时,就需要插入一个新的向量,否则直接在向量上插入节点的val

最后我们再翻转vec,即为所求答案

/**
* 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:
void breadthFirstSearch(TreeNode* root, vector<vector<int>> &vec)
{
if (!root)return;
queue<TreeNode*> que;
map<TreeNode*, int> ma;
que.push(root);
ma[root] = ;
while (!que.empty())
{
TreeNode* node = que.front();
if (node->left)
{
que.push(node->left);
ma[node->left] = ma[node] + ;
}
if (node->right)
{
que.push(node->right);
ma[node->right] = ma[node] + ;
}
if (vec.size() <= ma[node])
{
vector<int> childvec;
childvec.push_back(node->val);
vec.push_back(childvec);
}
else
{
vec[ma[node]].push_back(node->val);
}
que.pop(); }
}
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> vec;
breadthFirstSearch(root, vec);
reverse(vec.begin(), vec.end());
return vec;
}
};

LeetCode 107. Binary Tree Level Order Traversal II的更多相关文章

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

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

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

  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. (二叉树 BFS) 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 (二叉树阶层顺序遍历之二)

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

    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 二叉树的层次遍历 II

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

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

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

随机推荐

  1. nginx https http 共用

    openssl genrsa -des3 -out banmaxiaozhen.com.key 1024 openssl req -new -key banmaxiaozhen.com.key -ou ...

  2. html之ul标签

    html之无序列表,建议使用样式来定义列表的类型. 通常和li配对使用 可选属性: type:disc 圆点,circle圆圈,square方块 compact:显示效果比正常更小巧 <body ...

  3. Live Writer安装报错的问题,OnCatalogResult:0x80190194

    到官网下载了一个在线安装程序,可是一运行就提示无法安装,显式错误"OnCatalogResult:0x80190194",如下图所示   找到windows live安装程序的安装 ...

  4. Nginx 性能优化

    1.安全优化:隐藏Nginx版本号,server_tokens off; 2.安全优化:更改掉默认的用户  user nginx; 3.性能优化:  根据硬件配置,调整nginx worker 进程数 ...

  5. 使用原生js写ajax

    // 使用原生js 封装ajax // 兼容xhr对象 function createXHR(){ if(typeof XMLHttpRequest != "undefined") ...

  6. MySQL【Update误操作】回滚(转)

    前言:      继上一篇MySQL[Delete误操作]回滚之后,现在介绍下Update回滚,操作数据库时候难免会因为“大意”而误操作,需要快速恢复的话通过备份来恢复是不太可能的,因为需要还原和bi ...

  7. having 子句与where区别

    having 子句,条件子句 与 where 功能.用法相同,执行时机不同. where 在开始时执行检测数据,对原数据进行过滤. having 对筛选出的结果再次进行过滤. having 字段必须是 ...

  8. WAS维护常用操作

    0.WAS学习地址: http://www.open-open.com/doc/list/276?pn=1http://www.doc88.com/p-7498799200332.htmlhttp:/ ...

  9. SPOJ #2 Prime Generator

    My first idea was Sieve of Eratosthenes, too. But obviously my coding was not optimal and it exceede ...

  10. ulipad 常用快捷键

    快捷键名称 对应功能 F1 (M)UliPad Help Document(帮助文档) F2 (M)Directory Browser(目录浏览)(3.1版新增) F3 (M)Find Next(查找 ...