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

思路:广搜应该是普遍的方法了。还能避免栈溢出,多好用。搭配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. linux 输入子系统(4)---- input子系统的初始化

    Input子系统的初始化函数为input_init(),如下: static int __init input_init(void) { int err; input_init_abs_bypass( ...

  2. SQL Server是如何让定时作业

    如果在SQL Server 里需要定时或者每隔一段时间执行某个存储过程或3200字符以内的SQL语句时,可以用管理->SQL Server代理->作业来实现. 1.管理->SQL S ...

  3. win 7 64位如何安装erdas 9.2

    最主要的就是crack包必须包含这三个文件:erdas.exe、license.dat和lmgrd.exe 将这三个文件都复制到C盘安装目录下bin中,其余安装同win 7 32位系统

  4. 微软职位内部推荐-Senior SDE for Cloud Platform

    微软近期Open的职位: Microsoft Launched the public cloud service-Azure in China on May 2013, Cosmos is the k ...

  5. TWaver初学实战——如何在EasyUI中插入TWaver

    TWaver是一款强大的图形界面开发组件,可以很方便地集成到其他开发工具中.今天就用一个小例子来操练如何结合TWaver和EasyUI进行网页开发. 准备工作 俗话说他山之玉可以直接拿来,EasyUI ...

  6. Linux学习笔记(5)-进程管理

    进程简介 进程是正在执行的一个程序或命令,每一个进程都有自己的地址空间,并占有一定的系统资源.感性的认识,进程就是一个正在运行的程序 进程管理的作用 判断服务器的运行状态 查看系统中有哪些进程 杀死进 ...

  7. 1021: [SHOI2008]Debt 循环的债务 - BZOJ

    Description Alice.Bob和Cynthia总是为他们之间混乱的债务而烦恼,终于有一天,他们决定坐下来一起解决这个问题.不过,鉴别钞票的真伪是一件很麻烦的事情,于是他们决定要在清还债务的 ...

  8. Extjs4.2布局——layout: accordion(Ext.layout.container.Accordion)

    API这样介绍这种布局: 示例:(来自API)注:打开默认展开第一个面板. Ext.create('Ext.panel.Panel', { title: 'Accordion Layout', wid ...

  9. jQuery的优点——(一)

    稍微学点js然后就用起jQuery,用久了,就好像自己都成高手了,想得少了,都觉得自己就是js高手了.哗啦啦的惭愧啊=.= 面试GY的时候,因为在简历上写着大大的会用jQuery快速开发原型页面,然后 ...

  10. linux下执行 ls,cat等一些命令报出 -bash: /bin/cat: Cannot allocate memory 有没解决的方法

    环境变量配置出错了cd -- 进入用户目录vim .bash_profile删除以前PATH这一行,把下面的粘帖进去PATH=$PATH:$HOME/bin:/root:/root/snapshot/ ...