翻译

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

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

比如:
给定的二叉树是 {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. 【BZOJ4940】【YNOI2016】这是我自己的发明

    阅读此篇文章前请先跟我大喊三声:dllxl!dllxl!dllxl! 咳咳. 题意: Description 给一个树,n 个点,有点权,初始根是 1. m 个操作,每次操作: 1. 将树根换为 x. ...

  2. AC自动机笔记

    AC自动机 #include<iostream> #include<cstring> #include<cstdio> #include<cmath> ...

  3. PageUtil

    package cn.com.qmhd.oto.common; import java.io.Serializable; import java.util.List; import org.sprin ...

  4. fwupdate-efi 与 grub2-common 冲突

    在CentOS-7Minimal系统中使用命令如下命令yum groupinstall -y "GNOME Desktop"安装 图形界面时提示:fwupdate-efi 与 gr ...

  5. Mysql学习总结(23)——MySQL统计函数和分组查询

    1.使用count统计条数:select count(字段名...) from tablename; 2.使用avg计算字段的平均值:select avg(字段名) from tablename: 这 ...

  6. 【LeetCode OJ 34】Search for a Range

    题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...

  7. spark 数据预处理 特征标准化 归一化模块

    #We will also standardise our data as we have done so far when performing distance-based clustering. ...

  8. Redis允许远程连接

    默认安装启动redis后,只能本地连接,无法远程连接上,原因是redis安全策略默认本机访问,所以远程访问的话需要将 配置文件redis.cof 中的 bind 127.0.0.1 前面的#注释掉, ...

  9. [Codeforces 757E] Bash Plays with Functions (数论)

    题目链接: http://codeforces.com/contest/757/problem/E?csrf_token=f6c272cce871728ac1c239c34006ae90 题目: 题解 ...

  10. spring mvc给参数起别名

    需求: 将http报文请求(保护body和url)中的参数传递给Controller时支持使用别名. 举例: 下面两条请求报文的结果是一致的. http://example.com/foo?jobTy ...