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

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
/ \
9 20
/ \
15 7

return its level order traversal as:

[
[3],
[9,20],
[15,7]
]

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

思路:

重点在于每层元素个数不定,如何标记一层的结束,往堆栈里push很多NULL来表示空位这种方案,会造成Memory Limit Exceeded。

可以采取记下每层的NULL数量,下层翻倍这种方式计数,满额push标记NULL作为一层的结束

代码:

 vector<vector<int> > levelOrder(TreeNode *root) {
vector<vector<int> > orders;
if(root == NULL)
return orders; vector<int> vtmp;
queue<TreeNode*> tque;
tque.push(root);
tque.push(NULL); int size = ;
int count = ;
int zero = ;//该层的NULL数
while(!tque.empty()){
TreeNode * tmp = tque.front();//$$$$
tque.pop();//void pop() if(tmp == NULL){//NULL标识一层的结束
if(!vtmp.empty())//最后一行可能不满
orders.push_back(vtmp);
vtmp.clear();
continue;
}
vtmp.push_back(tmp->val);
if(tmp->left != NULL){
tque.push(tmp->left);
count++;
}
else
zero++;
if(tmp->right != NULL){
tque.push(tmp->right);
count++;
}
else
zero++; if(count + zero == size){
tque.push(NULL);
count = ;
size *= ;
zero = zero * ;
}
}
return orders;
}

【题解】【BT】【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. 15个让人惊讶的 CSS3 动画效果演示

    CSS 是网页设计非常重要的一部分,随着越来越多的浏览器对 CSS3 支持的不断完善,设计师和开发者们有了更多的选择.如今,用纯 CSS 就可以实现各种各样很酷的效果,甚至是动画. 本文收集了15个惊 ...

  2. DBCC DBREINDEX重建索引提高SQL Server性能

    大多数SQL Server表需要索引来提高数据的访问速度,如果没有索引,SQL Server 要进行表格扫描读取表中的每一个记录才能找到索要的数据.索引可以分为簇索引和非簇索引,簇索引通过重排表中的数 ...

  3. 完美实现自己的GetProcAddress函数(转载)

    我们知道kernel32.dll里有一个GetProcAddress函数,可以找到模块中的函数地址,函数原型是这样的: WINBASEAPI FARPROC WINAPI GetProcAddress ...

  4. IPTables系列:如何配置Ubuntu 14.04中的IPTables防火墙

    IPTables基本命令 在向大家介绍复杂防火墙规则之前,还是先上一些简单的料,让大家对IPTables最为基本的命令有一些简单了解. 首先要说明的是IPTables命令必需以root权限运行,这意味 ...

  5. Servlet上

    JavaWeb应用的概念 Java Web应用由一组Servlet.HTML页.类.以及其它可以被绑定的资源构成.它可以在各种供应商提供的实现Servlet规范的 Servlet容器 中运行. Jav ...

  6. C++-指针和引用的区别

    1,不存在空引用,指针可以为空 2,引用更高效,使用前不需要测试是否为空 3,指针可以被赋给别的对象,引用则不可以更改 总之,在对象有可能什么也不指向或者指向不同的对象的时候应该使用指针.

  7. dataTable/dataSet转换成Json格式

    using System.Text;using System.Collections.Generic; 1)dataTable转Json(表格有名称:dt.TableName) public stat ...

  8. STM32之延时秒,毫秒,微秒

    #include "delay.h" #include "stdint.h" #include "stm32f10x.h" ; //us延时 ...

  9. JS教程:词法作用域和闭包 (网络资源)

    varclassA = function(){ ; } classA.prototype.func1 = function(){ var that = this, ; function a(){ re ...

  10. UIView 翻转动画

    [_mapView removeFromSuperview]; [self addSubview:_tableView]; //应将self.view设置为翻转对象 [UIView transitio ...