【题解】【BT】【Leetcode】Binary Tree Level Order Traversal
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的更多相关文章
- 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 ...
随机推荐
- C#基础学习文章导航
第一部分:入个门 C#入门篇-1:HelloWorld的类 C#入门篇-2:什么是变量 C#入门篇-3:数据类型及转换 C#入门篇-4:使用运算符 第二部分:流程控制语句 C#入门篇5-1:流程控制语 ...
- DWR框架简单应用
各种Dwr简介不需要多说,知道是实现局部刷新就差不多了,至于实现原理,慢慢参透吧,一下说明怎么使用DWR 首先建一个web工程,然后添加如下jar包:
- How to Run a .Jar Java File
.jar files are used for archiving, archive unpacking. One of the essential features of jar file is l ...
- http错误和异常处理,认证和代理设置
http错误: import urllib.requestreq = urllib.request.Request('http://www.python.org/fish.html')try:urll ...
- uva 10491
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Asp.Net MVC3.0网站统计登录认证的在线人数
Asp.Net MVC3.0网站统计登录认证的在线人数 前言 对于一个网站来说,统计在线人数是一个很重要的工作.平时也发现很多的网站论坛等都有在线人数的显示.对于一个网站如果在线人数很多,用户看到了这 ...
- C#多线程学习之(五)使用定时器进行多线程的自动管理
本文实例讲述了C#多线程学习之使用定时器进行多线程的自动管理.分享给大家供大家参考.具体分析如下: Timer类:设置一个定时器,定时执行用户指定的函数. 定时器启动后,系统将自动建立一个新的线程,执 ...
- [CSS]三层嵌套的滑动门
原理: 最外层放水平平铺的背景,第二层放左边,第三层放右边,注意这个做法背景图不能透明 结构: <div class="module-title"> <span ...
- 两天以来对plsqldev操作的记忆
frist,开机后,打开服务,打开服务,打开服务(重要的事情说三遍). and then, 打开plsqldev,输入TEST账户而不是SYS账户,否则你会被许多未接触到的内容淹没你刚刚创建好的表. ...
- C#窗体的加载等待(BackgroundWorker控件)实现
窗体拉一个Button按钮和一个加载等待显示的label, label默认隐藏,点击按钮时显示这个label,加载完再隐藏 1.工具箱拉BackgroundWorker控件到窗体 2.backgrou ...