题意:按层,将元素收集在一个二维数组中。

思路:广搜应该是普遍的方法了。还能避免栈溢出,多好用。搭配deque,因为要经常删除。

 /**
* 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> > levelOrder(TreeNode* root) {
vector< vector<int> > ans;
if(!root) return ans;
deque<TreeNode*> que(,root);
while(!que.empty())
{
ans.push_back(vector<int>());
int siz=que.size();
while(siz--)
{
TreeNode* tmp=que.front(); if(tmp->left) que.push_back(tmp->left);
if(tmp->right) que.push_back(tmp->right); ans[ans.size()-].push_back(tmp->val);
que.pop_front();
}
}
return ans;
}
};

AC代码

LeetCode Binary Tree Level Order Traversal (按层收集元素)的更多相关文章

  1. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  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] Binary Tree Level Order Traversal 二叉树层序遍历

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

  4. LeetCode: Binary Tree Level Order Traversal 解题报告

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  5. [leetcode]Binary Tree Level Order Traversal II @ Python

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

  6. [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

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

  8. LeetCode——Binary Tree Level Order Traversal

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

  9. LeetCode: Binary Tree Level Order Traversal && Binary Tree Zigzag Level Order Traversal

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

随机推荐

  1. 数据可视化(三)- Seaborn简易入门

    本文内容来源:https://www.dataquest.io/mission/133/creating-compelling-visualizations 本文数据来源:http://www.cdc ...

  2. 源码编译安装LAMP环境及配置基于域名访问的多虚拟主机

    实验环境及软件版本: CentOS版本: 6.6(2.6.32.-504.el6.x86_64) apache版本: apache2.2.27 mysql版本:  Mysql-5.6.23 php版本 ...

  3. 求小于等于n的所有素数

    题目:要求输出所有小于等于n的素数(n>=2,且为正整数) 要求:1.每行输出10个素数 2.尽可能采用较优算法 #include<iostream> #include<cma ...

  4. (转)linux性能优化总结

    感谢博客http://sillycat.iteye.com提供的资料 linux性能检查(一)介绍和CPU 通常监控的子系统有: CPU Memory IO Network 应用类型 IO相关,处理大 ...

  5. EXTJS4.2 chart 柱状图

    chart 柱状图 Ext.require('Ext.chart.*'); Ext.require(['Ext.Window', 'Ext.fx.target.Sprite', 'Ext.layout ...

  6. bzoj 1318: [Spoj744] Longest Permutation 智商题

    1318: [Spoj744] Longest Permutation Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 361  Solved: 215 ...

  7. myeclipse报错:Could not create the view: An unexpected exception was thrown.

    打开server窗口,发现显示:Could not create the view: An unexpected exception was thrown. 此处解决方法: 关闭myeclipse 删 ...

  8. LA 4731

    dp[i][j]意思是前i个分成j组最小的花费 #include<cstdio> #include<algorithm> #include<cstring> #in ...

  9. hdu 4195

    我本来的想法求这个三角形的外心~~ 可以得到三条边对应圆心角   则这个正多边形的一条边对应的圆心角 应可以整除这三个角 但是一开始 没想到暴力枚举边数n,  还用模板求圆心坐标  然后求圆心角   ...

  10. [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计

    源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...