【题解】【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 ...
随机推荐
- go局部变量的存储空间是堆还是栈?
编译器会自动选择在栈上还是在堆上分配局部变量的存储空间,但可能令人惊讶的是,这个选择并不是由用var还是new声明变量的方式决定的. var global *int func f() { var x ...
- Windows 8 App: Information about CloudsCool Helper application
Website:http://www.cloudscool.com: App download address:http://dwz.cn/7DOJm: App English introduct ...
- toggleClass() 方法做类似于微信扣扣点击语音图标按钮变成切换到语音输入功能,点击键盘图标按钮切换到文字输入状态的效果
就是这种效果的类似. <html><head><script type="text/javascript" src="/jquery/jqu ...
- Excel 函数记录
1.四舍五入:round(数据,小数位数)
- ASP.NET MVC 上传大文件时404
前一段时间会员的上传组件改用FLASH的swfupload来上传,既能很友好的显示上传进度,又能完全满足大文件的上传. 后来服务器升级到windows 2008,改为IIS7后,上传文件一旦超过30M ...
- Time, Clocks, and the Ordering of Events in a Distributed System
作者:Leslie Lamport(非常厉害的老头了) 在使用消息进行通信的分布式系统中,使用物理时钟对不同process进行时间同步与事件排序是非常困难的.一是因为不同process的时钟有差异,另 ...
- Android的SwipeToDismiss第三方开源框架模拟QQ对话列表侧滑删除,置顶,将头像图片圆形化处理。
<Android SwipeToDismiss:左右滑动删除ListView条目Item> Android的SwipeToDismiss是github上一个第三方开源框架(github ...
- centos下cmake安装
步骤一.安装gcc等必备程序包(已安装则略过此步,用gcc -v检测) yum install -y gcc gcc-c++ make automake 步骤二.安装wget (已安装则略过此步) y ...
- java的动态绑定和静态绑定
首先是方法的参数是父类对象,传入子类对象是否可行然后引出Parent p = new Children();这句代码不是很理解,google的过程中引出向上转型要理解向上转型又引出了动态绑定从动态绑定 ...
- 使用变量替换批量部署GoldenGate
GoldenGate运行时允许在参数文件中动态指定一个值,即在参数文件中使用一个变量,而不是一个静态的值,当启动OGG进程时,根据环境动态加载此变量的值,达到在不同环境中,通过变量定义,实现多个环境的 ...