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

思路:广搜应该是普遍的方法了。还能避免栈溢出,多好用。搭配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学习系列之Linux入门(二)Vim学习

    第二篇 Vim学习 主要内容: 基本命令: 插件扩展: 参考资料: vim是一个命令控制的文本编辑器,可以完成几乎我们想要做的所有工作,除了Emacs几乎没有其他的工具能和它匹敌.官方网站是:http ...

  2. JSP页面时间动态显示 (转载)

    <script type="text/javascript">       function startTime(){        var today=new Dat ...

  3. 【git】学习路径失败了

    期初规划:搭建git远程服务器  使用gitlab作为管理工具 过程遇到的问题 1.gitlab不能安装到win ,且对centos要求6以上,我只有一台centos5  让运维帮升级 ...等待.. ...

  4. easyui 多行文本框 Multiline TextBox

    <input class="easyui-textbox" data-options="multiline:true" value="This ...

  5. collectionView代码创建

    @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> @p ...

  6. Elasticsearch升级至1.x后API的变化-三

    请支持原创:http://www.cnblogs.com/donlianli/p/3841762.html   1.索引格式 1.x之前的版本,被索引的文档type会同时出现在url和传输的数据格式中 ...

  7. C# Winform 涉及的拖放操作总结

    在开发程序的时候,为了提高用户的使用体验,或满足相关用户的功能,总是离不开拖放功能.而本文是总结winform下的常用拖放操作.主要有 1.textbox接受拖放的文件2.listbox允许用户自定义 ...

  8. [Windows Azure] Querying Tables and Entities

    Constructing Filter Strings When constructing a filter string, keep these rules in mind: Use the log ...

  9. replace()替换文字

    var test = text.innerHTML; b = test.replace(/任晓强/g,"你好"); console.log(b); html: <div id ...

  10. hdu 3646

    DP  状态转移方程还是比较容易想到  关键问题是当前要攻击的怪兽的血量 dp[i][j] = max(dp[i-1][j]+第i只鸟不使用double可杀死的怪兽数, dp[i-1][j-1]+第i ...