LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译
给定一个二叉树,返回从下往上遍历经过的每一个节点的值。
从左往右,从叶子到节点。
比如:
给定的二叉树是 {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)(*)的更多相关文章
- [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 ...
- leetcode 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II
相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
- Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS
题意是倒过来层次遍历二叉树 下面我介绍下BFS的基本框架,所有的BFS都是这样写的 struct Nodetype { int d;//层数即遍历深度 KeyType m;//相应的节点值 } que ...
- 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 ...
- (二叉树 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- [洛谷P2370]yyy2015c01的U盘
题目大意:有n个文件,每个文件有一个大小和价值,有一个容量为s的U盘,要装这些文件.传输文件需要接口,一个大小为k的接口能传输的最大文件的大小为k.问最少要多大的接口,才能使传输的文件价值$\ge p ...
- CF1000G Two-Paths (树形DP)
题目大意:给你一棵树,点有点权$a_{i}$,边有边权$w_{e}$,定义一种路径称为$2-path$,每条边最多经过2次且该路径的权值为$\sum _{x} a_{x}\;-\;\sum_{e}w_ ...
- RHEL8.0-beta-1.ISO
https://pan.baidu.com/s/1Yh_xuz39xGRrCtGtwhuqQg RHEL-8.0-beta-1-x86_64-dvd.iso 文件名: E:\rhel-8. ...
- 使用vue实现简单键盘,支持移动端和pc端
常看到各种app应用中使用自定义的键盘,本例子中使用vue2实现个简单的键盘,支持在移动端和PC端使用,欢迎点赞,h5 ios输入框与键盘 兼容性优化 实现效果: Keyboard.vue <t ...
- 紫书 习题11-11 UVa 1644 (并查集)
这道题感觉思路非常巧妙, 我是看了别人的博客才想明白的. 这里用到了并查集, 以根节点为中心城市, 然后把边从大到小排序, 每次的当前的边即为容量, 因为是目前的最小值, 然后去算总的容量, 每次选容 ...
- Linux学习总结(12)——Linux必须学会的60个命令
Linux系统信息存放在文件里,文件与普通的公务文件类似.每个文件都有自己的名字.内容.存放地址及其它一些管理信息,如文件的用户.文件的大小等. 文件可以是一封信.一个通讯录,或者是程序的源语句.程序 ...
- 小于等于N的全部整数与N关于gcd(i,N)的那些事
相关问题1: 求小于等于N的与N互质的数的和.即∑ i (gcd(i,N)=1, N>=i>0) 依据N的规模能够有非常多种方法.这里我介绍一个比較经典的方法 先说下这个结论:假设 gcd ...
- Linux Shell脚本编程学习笔记和实战
http://www.1987.name/141.html shell基础 终端打印.算术运算.经常使用变量 Linux下搜索指定文件夹下特定字符串并高亮显示匹配关键词 从键盘或文件里获取标准输入 [ ...
- C语言中static的使用
在开发过程中.我们常常会须要定义一些static类型的变量或者函数.我们接下来来详细聊一下static: 1.修饰变量 当static来修饰一个变量时,就注定了这个变量的可见范围和生命周期: (1)当 ...
- NSTimer解除循环引用
NSTimer作为一个经常使用的类,却有一个最大的弊病,就是会强引用target.造成调用timer很麻烦.稍有不慎就造成内存泄漏. 下面就是为解决问题做的封装. 直接上代码: #import < ...