LeetCode() Binary Tree Level Order Traversal
感觉我这个思路好 先记录上一层有几个节点
/**
* 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>> res;
vector<int> path;
queue<TreeNode*> q;
if(!root) return res;
q.push(root);
while(!q.empty()){
int len=q.size();
while(len--){
TreeNode* tem=q.front();
q.pop();
path.push_back(tem->val); if(tem->left!=NULL) q.push(tem->left);
if(tem->right!=NULL) q.push(tem->right);
}
res.push_back(path);
path.clear();
}
return res;
}
};
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 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 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 与 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 II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
随机推荐
- windows下python Tkinner环境布置(包含PIL环境安装)
布置步骤:1.安装python 2.7.11 安装步骤:由于网上存在有相关经验,所以在此引用一下 http://jingyan.baidu.com/article/0bc808fc42dfab1bd4 ...
- Loadrunner11安装和破解方法
公司很多项目都在做性能测试,打算把性能测试学习下.(不懂还可以问问公司大神,这么好的机会不要错过了O(∩_∩)O哈哈~)用了二周实践看了性能测试方面一些基本术语和概念,一直都还没自己动手实践,光看基本 ...
- 【转】输入/输出流 - 深入理解Java中的流 (Stream)
基于流的数据读写,太抽象了,什么叫基于流,什么是流?Hadoop是Java语言写的,所以想理解好Hadoop的Streaming Data Access,还得从Java流机制入手.流机制也是JAVA及 ...
- 官网app下载更换成微信公众号二维码 测试
微信现在很火啊.公司官网原先提供的ios和andriod的app下载链接要求切换成微信公众号二维码.简单的替换,大家都说不需要测试直接上线.还是测了下. 1 验证所有与下载相关的信息都已去除. 包括下 ...
- 第三章 Git使用入门
我们都知道Linux和Android是开源的.Linux下的软件很多都不直接以二进制形式的安装包提供,而是直接提供了源代码,为了减少发行包的大小,用户须先下载源代码,在本机上编译并安装,使用make. ...
- 安全协议系列(二)----CCM与CCMP
CCMP(CTR with CBC-MAC Protocol) 是 IEEE 802.11i 中推出使用基于 AES 的 CCM 模式的安全加密协议.与原来脆弱的 WEP 算法及临时补救措施 TKIP ...
- Fragment的生命周期(三)
自定义lifecycleoffragment布局文件 在main_activity布局中引用自定义的fragment布局 到logcat中查看程勋运行的结果 代码如下: 自定义的fragment布局: ...
- freemarker string= null
在java代码中经常会出现以下代码: String name; …………………… if(null == name || name.length == 0){ return; } 这行代码用freema ...
- SQL Server简单语句/待整理
数据库对象:表Table,视图View,存储过程Stored Procedure,触发器Trigger 关系:1关系=1二维表,1关系有1关系名.1关系=1表对象 属性/字段: 二维表中垂直方向的列 ...
- Ubuntu 使用Cisco VPN、AnyConnect、OpenConnect的方法。
自己建的廉价Shadowsocks服务器总是不稳定,众所周知,PPTP在中国大陆已经废了.为了连接外网,所以购买了BlueCloud的VPN.但是他们家的VPN是使用Cisco VPN,可以使用Any ...