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. ASP.NET MVC中的错误-友好的处理方法

    转自:http://blog.csdn.net/lizhao1226/article/details/6367400 “/”应用程序中的服务器错误. 无法找到资源. 说明: HTTP 404.您正在查 ...

  2. yii 主从数据库分离-转载http://www.yiichina.com/doc/guide/2.0/db-dao

    数据库复制和读写分离 很多数据库支持数据库复制 database replication来提高可用性和响应速度. 在数据库复制中,数据总是从主服务器 到 从服务器. 所有的插入和更新等写操作在主服务器 ...

  3. Protocol Buffer Java实例

    大家要先下载protobuffer,在这里: https://code.google.com/p/protobuf/downloads/list 注意,需要下载两个,一个是complier,另外一个是 ...

  4. mootools upgrate from 1.2 to 1.3 or 1.4

    Update from 1.2 to 1.3 lorenzos edited this page on 8 Jul 2011 · 2 revisions Pages 19 Home Home Chan ...

  5. github 添加 C# IGNORE

    在创建仓库时选择 VisualStudio 即可.

  6. 剑指offer系列28--字符流中第一个不重复的字符

    [题目]请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符”go”时,第一个只出现一次的字符是”g”.当从该字符流中读出前六个字符“google”时,第一个只出现 ...

  7. FullCalendar

    一个非常完美的日期控件:https://fullcalendar.io/

  8. FastReport使用二——二维码

    以下内容在FastReport Designer 中测试通过,如下图所示: 在使用FastReport Designer创建一维吗也就是一般普通的条码时,设置其Barcode属性为Code128 (建 ...

  9. [内核同步]自旋锁spin_lock、spin_lock_irq 和 spin_lock_irqsave 分析

    转自:http://blog.csdn.net/wh_19910525/article/details/11536279 自旋锁的初衷:在短期间内进行轻量级的锁定.一个被争用的自旋锁使得请求它的线程在 ...

  10. 75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...