感觉我这个思路好 先记录上一层有几个节点

/**
* 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的更多相关文章

  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 II @ Python

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

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

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

  6. [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 ...

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

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

  9. LeetCode——Binary Tree Level Order Traversal

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

  10. LeetCode - Binary Tree Level Order Traversal II

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

随机推荐

  1. strace命令使用

    在调试的时候,strace能帮助你追踪到一个程序所执行的系统调用.当你想知道程序和操作系统如何交互的时候,这是极其方便的,比如你想知道执行了哪些系统调用,并且以何种顺序执行. 这个简单而又强大的工具几 ...

  2. android 代码优化

    http://android.tgbus.com/Android/androidnews/200812/172247.shtml http://blog.163.com/jzq_520/blog/st ...

  3. 几个功能强大的系统源码(机票分销、机票预订、OA、手机充值、wifi营销、网络超市、体检平台)

    1.机票分销.机票预订系统源码 2.OA系统源码 3.手机在线充值系统源码 4.wifi营销系统源码 5.网络超市系统源码 6.在线体检平台系统源码 7.违章查询与缴费系统源码 需要的同学请联系QQ: ...

  4. Cairo 下载,测试

    You need to download the all-in-one bundle available here. You can discover this link yourself by vi ...

  5. PHP面试题之驼峰字符串转换成下划线样式例子

    自己在看到这个问题的时候,想到的是用ASCII码来处理,没往万能的正则上去想.好吧,下面来看看答案: 答案1: 代码如下 复制代码 $str = 'OpenAPI'; $length = mb_str ...

  6. JS第二天简单总结

    布尔型变量:true,flase 广泛用于真假的逻辑判断 对象:分类,对象是由一些彼此相关的属性和方法集合在一起而构成的一个数据实体: 例如,var today = new Date();中,Date ...

  7. C# treeview 绑定数据 【转】

    private void bindTreeView1() { string sql = "select * from dm_category"; DataTable dt = db ...

  8. UE4 WCF RestFul 服务器 读取JSON 数据并解析 简单实例

    Note:不知道为什么通过Txt读取的JsonString,如果TXT 不是ANSI编码的话,会报JsonArrayStringToUStruct  Unable to parse. bool UWg ...

  9. Ubuntu 16.04 64位安装insight 6.8

    1. apt-get install insight已经不管用. 2. 编译源码死都有问题. 3. 拜拜,用KDBG.

  10. Amd64 and Va_arg

    Made of Bugs Blog Archives Author Amd64 and Va_arg OCT 3RD, 2010 A while back, I was poking around L ...