LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)
题意:从左到右统计将同一层的值放在同一个容器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 (二叉树颠倒层序)的更多相关文章
- [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 ...
- [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 ...
- [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]Binary Tree Level Order Traversal II @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...
- 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 ...
- 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, ...
- LeetCode - Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 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, ...
随机推荐
- BZOJ 3436: 小K的农场 差分约束
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=3436 题解: 裸的差分约束: 1.a>=b+c -> b<=a-c ...
- execvp使用实例
问题描述: 本程序实现模拟shell功能,用户输入命令,返回相应的结果 问题解决: 注: 以上指出了execvp函数的使用,使用时第一个参数是文件名,第二个参数是一个 ...
- 【娱乐】高端小游戏Manufactoria
Manufactoria 是一款游戏.游戏中,一家生产机器人的工厂内部出了一 些问题,生产出来的机器人有很多不合格的.一个机器人可以用一个含有红色和 蓝色的颜色串来描述,只有颜色串符合某种规律的机器人 ...
- NYOJ-171 聪明的kk AC 分类: NYOJ 2014-01-02 09:01 165人阅读 评论(0) 收藏
#include<stdio.h> #define max(x,y) x>y?x:y int main(){ int num[22][22]={0}; int n,m; int x, ...
- windowopen
1.最基本的弹出窗口代码] <SCRIPT LANGUAGE="javascript"> <!-- window.open ('page.html') --> ...
- c# 与 Unity3d 中的序列化
圣典中对于Unity3D的序列化介绍很容易和C#的序列化介绍搞混,做个笔记,方便以后查找. 很多资料算是拾人牙慧. 一.Serializable 序列化 Inherits from Attribute ...
- Unity3D 问题流水总结
一.error CS1612:Cannot modify a value type return value of `UnityEngine.SliderJoint2D.limits'. Consid ...
- ios iap 购买总是提示继续的解决方案
原地址:http://blog.csdn.net/kafeidev/article/details/8619984 ========================================== ...
- select count的优化
select count的优化 2011-08-02 12:01:36 分类: Oracle 一般情况下,select count语句很难避免走全表扫描,对于上百万行的表这个语句使用起来就比较吃力了, ...
- PowerDesigner 逆向工程 从SQL文件转换成PDM 从PDM转成CDM
从SQL文件逆向工程到PDM: ①选择file -> Reverse Engineer - > Database ②在General选项卡中选择MySQL数据库,点击确定. ③using ...