翻译

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

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

比如:
给定的二叉树是 {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. pandaboy玩pandas

    基于python的三方库pandas的excel表二次开发 import numpy as np import pandas as pd import time from pandas import ...

  2. 简洁的MVC思想框架——Nancy(环境配置与Get操作)

    Nancy官网——https://github.com/NancyFx/Nancy 概述:Nancy是一个开源的Web轻型框架内核符合MVC思想,有开发方便,路由简单的特点,而且功能齐全 起步:Hel ...

  3. iOS基础UI控件介绍-Swift版

    iOS基础UI控件总结 iOS基础控件包括以下几类: 1.继承自NSObject:(暂列为控件) UIColor //颜色 UIImage //图像 2.继承自UIView: 只能相应手势UIGest ...

  4. solr + eclipse 调试环境搭建

    1: 在官网下载对应源码 http://www.fayea.com/apache-mirror/lucene/solr/4.1.0/ 选择源码文件,如图所示: 2: 解压后目录如图所示: 在根目录下存 ...

  5. 最强最全干货分享:Android开发书籍、教程、工具等

    最全干货分享,本文收集整理了Android开发所需的书籍.教程.工具.资讯和周刊各种资源,它们能让你在Android开发之旅的各个阶段都受益. 入门<Learning Android(中文版)& ...

  6. Project Euler:Problem 93 Arithmetic expressions

    By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four ari ...

  7. d堆

    就是d叉堆,是二叉堆的简单推广(http://blog.csdn.net/buleriver/article/details/38469907) 对于一个d堆.也是能够使用数组表示.关键是怎样通过索引 ...

  8. nginx 1.5 支持websocket

    proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set ...

  9. SharePoint Search之(七)Search result- 结果源

    在使用搜索引擎的时候.非常多情况下,用户希望限定一下搜索范围,以便更加easy找到想要的结果. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU1BGYXJ ...

  10. http --- 从输入URL到页面加载的过程发生了什么?

    可以分为这几个大的过程: DNS解析 TCP连接 客户端发送HTTP请求 服务器处理请求并返回HTTP报文 浏览器解析渲染页面 结束 其中(1)DNS解析可以理解为主寻找这个IP地址的过程,其中如果找 ...