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,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

[
[15,7],
[9,20],
[3]

]

这个题目要采用广度优先遍历,所以我们需要一个队列,为了记录节点的深度我用了一个map映射来记录节点的深度

当vec的长度小于节点的深度时,就需要插入一个新的向量,否则直接在向量上插入节点的val

最后我们再翻转vec,即为所求答案

/**
* 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:
void breadthFirstSearch(TreeNode* root, vector<vector<int>> &vec)
{
if (!root)return;
queue<TreeNode*> que;
map<TreeNode*, int> ma;
que.push(root);
ma[root] = ;
while (!que.empty())
{
TreeNode* node = que.front();
if (node->left)
{
que.push(node->left);
ma[node->left] = ma[node] + ;
}
if (node->right)
{
que.push(node->right);
ma[node->right] = ma[node] + ;
}
if (vec.size() <= ma[node])
{
vector<int> childvec;
childvec.push_back(node->val);
vec.push_back(childvec);
}
else
{
vec[ma[node]].push_back(node->val);
}
que.pop(); }
}
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> vec;
breadthFirstSearch(root, vec);
reverse(vec.begin(), vec.end());
return vec;
}
};

LeetCode 107. Binary Tree Level Order Traversal II的更多相关文章

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

  2. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

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

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

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

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

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

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

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

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

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

随机推荐

  1. jquery操作html data-* 属性的坑

  2. jquery的$.extend和$.fn.extend作用及区别.txt

    jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); (1)类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax ...

  3. Genymotion出现Unable to load VirtualBox engine问题--100%解决

    首先强调一个事情,作为程序员,一定要养成一个习惯:安装软件时尤其像VirtualBox这类,首先设置其"兼容模式"以及"特权等级" . 环境:win7 64bi ...

  4. [mysql] linux 下mysql 5.7.12 安装

    1.下载mysql wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar ...

  5. 多线程编程之Windows环境下创建新线程

    转自: http://www.cnblogs.com/lgxqf/archive/2009/02/10/1387480.html 在 Win32 API 中,创建线程的基本函数是 CreateThre ...

  6. Jaxb笔记

    摘自: http://www.blogjava.net/eagle-daiq/archive/2012/01/30/369016.html 最近项目原因,研究了下jaxb.jaxb是Java api ...

  7. IDrac的console无法键盘输入

    IDrac的console无法键盘输入问题? 解:disable IE 的protect 功能 (Idrac的正常工作需要先安装Java,同时IDrac只支持IE和Firefox.) 方法: IE-& ...

  8. linux中cat、more、less命令区别详解

    众所周知linux中命令cat.more.less均可用来查看文件内容,主要区别有:cat是一次性显示整个文件的内容,还可以将多个文件连接起来显示,它常与重定向符号配合使用,适用于文件内容少的情况:m ...

  9. [tty与uart]理解线路规程的作用

    转自:http://biancheng.dnbcw.info/linux/336240.html Linux OS的设备驱动有相当经典的抽象思想以及分层思想.与通信世界里面的思想相一致. 一.在Lin ...

  10. IO - FileUtils

    Apache Commons IO好用的功能主要集中在工具类FileUtil中,包含了建立,删除,复制,移动,比较文件新旧,递归枚举目录清空目录,一次读取整个文件等.以下是一个我认为有用的列表: 1. ...