翻译

给定一个二叉树,返回从下往上遍历经过的每一个节点的值。

从左往右,从叶子到节点。

比如:
给定的二叉树是 {3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
返回它从下往上的遍历结果:
[
[15,7],
[9,20],
[3]
]

原文

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

分析

事实上吧,无论是从上到下还是从下到上都无所谓啦。最后反转一下就好了,关键还是在于怎样去遍历。

我一開始没理解好题意。结果是按节点以下的两个叶子来加入到vector的,后来发现原来是应该按层级。

所以採用了先进先出的队列,队列里要包括二叉树的层级信息。所以构造一个pair。

vector<vector<int>> vecAns;
if (!root) return vecAns;
queue<pair<int, TreeNode*>> queueTree;

首先定义了用于最后返回的vecAns,而后推断root是否为空,是的话直接返回不做加入操作。构造的queue中int用于存放层级信息,TreeNode*用于存放节点。

接下来定义了map。它的优势在于能够随时指定键来加入值,这里就是指定层级来加入信息,后面的是vector就是用于存放树节点的。root的层级设定为0。后面用make_pair来构造pair对。最后加入到queue中。

map<int, vector<int>> mapAns;
int rootLevel = 0;
queueTree.push(make_pair(rootLevel, root));

仅仅要queue不为空就一直循环。

每次一開始就解析出当前队列顶部的层级信息以及当前节点。将它加入到map中。加入完之后就能够弹出了。继续推断左右子树,假设为空就先加入到queue中等待下一部操作。待到下一次循环时,就是将它们加入到map中了。

while (!queueTree.empty()) {
int currentLevel = (queueTree.front().first);
TreeNode *currentNode = (queueTree.front().second);
mapAns[currentLevel].push_back(currentNode->val);
queueTree.pop();
if (currentNode->left != NULL)
queueTree.push(make_pair(currentLevel + 1, currentNode->left));
if (currentNode->right != NULL)
queueTree.push(make_pair(currentLevel + 1, currentNode->right));
}

将map中的信息逐个push到vector里,最后就直接return了。

for (auto iter = mapAns.rbegin(); iter != mapAns.rend(); ++iter) {
vecAns.push_back(iter->second);
}
return vecAns;

Ok,大家能够去看看上一题:

LeetCode 102 Binary Tree Level Order Traversal(二叉树的层级顺序遍历)(*)

代码

/**
* 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) {
vector<vector<int>> vecAns;
if (!root) return vecAns;
queue<pair<int, TreeNode*>> queueTree;
map<int, vector<int>> mapAns;
int rootLevel = 0;
queueTree.push(make_pair(rootLevel, root));
while (!queueTree.empty()) {
int currentLevel = (queueTree.front().first);
TreeNode *currentNode = (queueTree.front().second);
mapAns[currentLevel].push_back(currentNode->val);
queueTree.pop();
if (currentNode->left != NULL)
queueTree.push(make_pair(currentLevel + 1, currentNode->left));
if (currentNode->right != NULL)
queueTree.push(make_pair(currentLevel + 1, currentNode->right));
}
for (auto iter = mapAns.rbegin(); iter != mapAns.rend(); ++iter) {
vecAns.push_back(iter->second);
}
return vecAns;
}
};

LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)的更多相关文章

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

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

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

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

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

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

  5. (二叉树 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 ...

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

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

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

随机推荐

  1. 玩转HTML5移动页面(优化篇)

    标签:h5 页面优化收藏 热门分享 网页设计师必备的 酷站收藏网站 2013年不容错过的app ui素材 40个漂亮的扁平化网页设计欣赏 国内网页设计公司网站欣赏 55套网页设计常用的psd格式UI ...

  2. kali(Ubuntu)右键添加idle打开方式

    IDLE可以说是Unix平台下Python的第一个集成开发环境(IDE) 命名行输入idle看idle是否已安装,没有则先安装 安装idle:apt-get install idle 安装完成后,命名 ...

  3. (四)React高级内容

    1. React developertools安装及使用 2. PropTypes与DefaultProps 讲一下PropTypes, 先拿TodoItem来说: 从几种类型中选: 3 props ...

  4. 前端路由的两种模式:hash(#)模式和history模式(转)

    随着 ajax 的使用越来越广泛,前端的页面逻辑开始变得越来越复杂,特别是spa的兴起,前端路由系统随之开始流行. 从用户的角度看,前端路由主要实现了两个功能(使用ajax更新页面状态的情况下): 记 ...

  5. rem — 一个低调的css单位

    原文  http://www.zhaoan.org/1825.html rem这是个低调的 css 单位,近一两年开始崭露头角,有许多同学对rem的评价不一,有的在尝试使用,有的在使用过程中遇到坑就弃 ...

  6. yii框架原生代码

    http://www.cnblogs.com/duanxz/p/3480254.htm

  7. 捕捉到来自宇宙深空的神奇X-射线信号

    请看下图: 这是专门用于捕捉X-射线信号的航天望远镜,约有5吨重,执行轨道距离地面大约有5万多公里.6月24日,美国宇航局NASA宣布,这台航天望远镜从银河系深处捕捉到一种波长非常特殊的神奇X-射线信 ...

  8. sass06 mixin

    scss @mixin cont{ //mixin是关键字 color:red; } body{ @include cont; //使用默认值 } @mixin cont($color: red ){ ...

  9. 11_HTML5_Local_Storage本地存储

    本地存储localStorage是大型cookie,cookie只有4k,

  10. BZOJ5042: LWD的分科岛

    [传送门:BZOJ5042] 简要题意: 给出n个数,q个询问,每个询问输入opt,l,r,如果opt=1,则输出l到r中的最小值,否则输出最大值 题解: 直接上ST表,自信一波,结果 MLE??好吧 ...