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 ...
随机推荐
- 使用Python进行多线程检查.moe三位剩余有效域名
翻看博客看到一段不错的代码 虽然近期没有购买域名的需求 不过日后有购买域名的需求的话 稍作修改直接使用还是很方便的 import threading import requests import js ...
- es6 学习7 Set 和 Map 数据结构
Set 和 Map 数据结构 一.Set ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. const set = new Set([1, 2, 3, 4, ...
- 你可能需要了解下Laravel集合
前言 集合通过 Illuminate\Support\Collection 进行实例,Laravel的内核大部分的参数传递都用到了集合,但这并不代表集合就是好的.Laravel作为快捷并优雅的开发框架 ...
- numpy学习笔记 - numpy数组的常见用法
# -*- coding: utf-8 -*- """ 主要记录代码,相关说明采用注释形势,供日常总结.查阅使用,不定时更新. Created on Mon Aug 20 ...
- SpringBoot项目maven 打包时跳过测试
在打包spring boot项目时,如果测试用例特别多,打包时间会增加: 而且测试用例有时忘记了做相应修改,在打包时则会报错而终止打包,就很烦. 所以这时会想在打包时跳过测试,大致有2种方法: 方法一 ...
- mysql索引的使用及优化方法
数据库高级管理及优化 MySQL性能优化 优化MySQL数据库是数据库管理员和数据库开发人员的必备技能.优化MySQL,一方面是找出系统的瓶颈,提高MySQL数据库整体的性能:另一方面是合理设计结构和 ...
- [Angular + TsLint] Disable directive selector tslint error
@Directive({ // tslint:disable-next-line:directive-selector selector: '[scrollable]' })
- Linux 6.3下安装Oracle Enterprise Cloud Control 12c
Oracle enterprise cloud control 12c的安装是一个比較复杂的过程,由于他须要依赖于Oracel database以及Oracle Weblogic. 如今Oracle已 ...
- iOS_青花瓷Charles抓包
使用青花瓷Charles抓取手机端的网络请求: 第一步,下载安装并打开Charles watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5l ...
- oracle 存储过程定义及调试,并终于被C# 调用 代码
C# 调用存储过程 參考了非常多文章,写了例如以下文字,算是分享吧 目的:更改积分,并作一定校验 一.一般的调试方法: 方法一:带返回out參数,必须定义变量 myresult DECLARE myr ...