LeetCode Binary Tree Level Order Traversal (按层收集元素)
题意:按层,将元素收集在一个二维数组中。
思路:广搜应该是普遍的方法了。还能避免栈溢出,多好用。搭配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 (按层收集元素)的更多相关文章
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- [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 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode: Binary Tree Level Order Traversal 解题报告
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- [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 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- [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
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 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 ...
随机推荐
- 简单的异步任务工具——rq 的使用教程
rq是一个简单的,轻量级的异步任务工具. 如果在网站中用户发起一个用时很久(大于2分钟)的请求,如果用同步的方式,服务器就会返回超时. 这时候就需要用异步请求,用户发起请求后,服务端把作业扔给另一个进 ...
- 回车,根据编码获取相应记录,然后再将这录绑定到AutoList
问题描述: 回车后,根据编码获取相应记录,然后再将这录绑定到AutoList(我们自定义控件,其实就是下拉列表),回车事件是用jquery ajax实现,这样在后台给AutoList绑定数据源,如果不 ...
- easy ui 菜单和按钮(Menu and Button)
http://www.zi-han.net/case/easyui/menu&button.html
- 总有你需要的之 ios 小技巧 (下)
图片上绘制文字 写一个UIImage的category NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultPara ...
- Xcode8 重新配置 CocoaPods -替换阿里源
一.CocoaPods是什么? CocoaPods是一个负责管理iOS项目中第三方开源库的工具.CocoaPods的项目源码在Github上管理.该项目开始于2011年8月12日,在这两年多的时间里, ...
- 轻仿QQ音乐之音频歌词播放、锁屏歌词-b
先上效果图 歌词播放界面 音乐播放界面 锁屏歌词界面 一. 项目概述 前面内容实在是太基础..只想看知识点的同学可以直接跳到第三部分的干货 项目播放的mp3文件及lrc文件均来自QQ音乐 本文主要主要 ...
- Oralce11 客户端的安装和 PlSql Developer 的配置
关于Oracle11服务器端安装时的配置问题我就不讲了,就是要安装DataBase1和DataBase2. 现在我来讲的是Oralce11 客户端的安装和PlSql的配置问题: 步骤一:选择图示,wi ...
- [转载]VS2012创建MVC3项目提示错误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。
如果在没有安装vs2012 update3升级包的情况下,创建MVC3项目会出现下面的错误信息. 因为VS2012已经全面切换到使用NuGet这个第三方开源工具来管理项目包和引用模块了,使用VS201 ...
- hdu 1028
递推 #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> ...
- iOS设计模式——委托(delegate)
委托(delegate)也叫代理是iOS开发中常用的设计模式.我们借助于protocol(参考博文:objective-c协议(protocol))可以很方便的实现这种设计模式. 什么是代理? 苹果的 ...