题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒。

思路:广搜逐层添加进来,最后再反转。

 /**
* 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> > ans;
if(!root) return ans;
vector<int> tmp;
deque<TreeNode * > que;
que.push_back(root);
while(!que.empty()) //广搜
{
int siz= que.size();
tmp.clear();
for(int i=; i<siz; i++)
{
TreeNode* v=que.front();que.pop_front();
tmp.push_back(v->val);
if(v->left) que.push_back(v->left);
if(v->right) que.push_back(v->right);
}
ans.push_back(tmp);
}
reverse(ans.begin(), ans.end());
return ans;
}
};

AC代码

LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)的更多相关文章

  1. [LeetCode] 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] Binary tree level order traversal ii二叉树层次遍历

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

  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. [leetcode]Binary Tree Level Order Traversal II @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...

  5. LeetCode——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 102. Binary Tree Level Order Traversal(二叉树的层序遍历)

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

  7. LeetCode - Binary Tree Level Order Traversal II

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

  8. [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历

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

  9. LeetCode OJ:Binary Tree Level Order Traversal(二叉树的层序遍历)

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

随机推荐

  1. github 中删除/更名版本库(repository)

    问题描述: github 中版本库创建/删除/更该名称 问题解决:            (1)创建版本库(Repository) 注:        在上图中的+按钮图标指示的是创建版本库的按钮 注 ...

  2. Google 网站打不开

    http://209.116.186.246/ http://91.213.30.153/  (2014年6月30日 新增) https://wen.lu/ (2014年6月30日 新增,注意下是ht ...

  3. Feature Flag

    know more from here: https://www.youtube.com/watch?v=WMRjj06R6jg&list=UUkQX1tChV7Z7l1LFF4L9j_g F ...

  4. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

  5. IE浏览器 下面的文本框,获得焦点后无法输入内容

    今天遇到一个问题,在IE浏览器下面,我点击 按钮  弹出一个弹出层,里面有一个 文本编辑器和一个文本框,但是第二次弹出后,文本框和文本编辑器无法输入内容,在控制台用js代码测试 $(document) ...

  6. PowerDesigner 逆向工程 从SQL文件转换成PDM 从PDM转成CDM

    从SQL文件逆向工程到PDM: ①选择file -> Reverse Engineer - > Database ②在General选项卡中选择MySQL数据库,点击确定. ③using ...

  7. Sina App Engine(SAE)入门教程(9)- SaeMail(邮件)使用

    参考资料: SAE mail api 文档 怎么使用? 参见代码: <?php $mail = new SaeMail(); $f = new SaeFetchurl(); $img_data ...

  8. 致诸位新程序员:来自Chuck Jazdzewski慈父般的忠告

    记住这几句话,学无止境.(Never stop learning.)沟通至关重要.(Communication is critical.)履行承诺,胜过交付.(Under promise, over ...

  9. Qt学习记录--Qt::CaseSensitive

    Qt::CaseSensitivity 为枚举类型, 可取值Qt::CaseSensitive 和 Qt::CaseInsensitive, 表示匹配的灵敏度. 比较字符串的时候 Qt::CaseSe ...

  10. MSSQL2005数据库自动备份问题(到同一个局域网上的另一台电脑上)

    有A数据库服务器,B本机:    我现在想通过在B机器上通过代码调用SQL来执行A数据库的备份到B机器上    调用的SQL语句为:Backup Database MYDATABASE To Disk ...