LeetCode OJ--Binary Tree Level Order Traversal
http://oj.leetcode.com/problems/binary-tree-level-order-traversal/
树的层序遍历,使用队列
由于树不是满的,还要分出每一层来,刚开始给缺少的节点用dummy节点代替,结果超时了。
vector<vector<int> > levelOrder(TreeNode *root) {
vector<vector<int> > ans;
if(root == NULL)
return ans;
int num = ,num2 = ;
queue<TreeNode *> myQueue;
myQueue.push(root);
TreeNode *nodeFront;
TreeNode *dummy = new TreeNode(-);
vector<int> onePiece;
while(!myQueue.empty())
{
nodeFront = myQueue.front();
myQueue.pop();
num--;
if(nodeFront != dummy)
{
onePiece.push_back(nodeFront->val);
if(nodeFront->left)
myQueue.push(nodeFront->left);
else
myQueue.push(dummy);
if(nodeFront->right)
myQueue.push(nodeFront->right);
else
myQueue.push(dummy);
}
else
{
myQueue.push(dummy);
myQueue.push(dummy);
}
if(num == )
{
if(onePiece.empty())
break;
ans.push_back(onePiece);
onePiece.clear();
num2 = num2*;
num = num2;
}
}
return ans;
}
改进的话,对缺失的节点进行计数,则计算出下一层应该有多少个节点来,如下。
vector<vector<int> > levelOrder(TreeNode *root) {
vector<vector<int> > ans;
if(root == NULL)
return ans;
int num = ,num2 = ,nullNum = ,nullNumAcc = ;
queue<TreeNode *> myQueue;
myQueue.push(root);
TreeNode *nodeFront;
vector<int> onePiece;
while(!myQueue.empty())
{
nodeFront = myQueue.front();
myQueue.pop();
num--;
onePiece.push_back(nodeFront->val);
if(nodeFront->left)
myQueue.push(nodeFront->left);
else
nullNum++;
if(nodeFront->right)
myQueue.push(nodeFront->right);
else
nullNum++;
if(num == )
{
if(onePiece.empty())
break;
ans.push_back(onePiece);
onePiece.clear();
num2 = num2*;
nullNumAcc = nullNumAcc* + nullNum;
num = num2 - nullNumAcc;
nullNum = ;
}
}
return ans;
}
LeetCode OJ--Binary Tree Level Order Traversal的更多相关文章
- Java for LeetCode 107 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 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode(32)-Binary Tree Level Order Traversal
题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...
- [LeetCode] 102. 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 I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【题解】【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, ...
- leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
随机推荐
- 【wqs二分】HHHOJ#15. 赤
这个wqs二分并不熟练…… 题目描述 #15. 赤 题目分析 两维都用wqs二分,其他没有什么特殊之处. 重点在于,wqs二分还原最优解的时候,增量是强制给的k. #include<bits/s ...
- shell 练习题
1.编写脚本/bin/per.sh,判断当前用户对指定参数文件,是否不可读并且不可写 read -p "Please Input A File: " file if [ ! -e ...
- shell中变量字符串的截取 与 带颜色字体、背景输出
字符串截取 假设我们定义了一个变量为:file=/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值:${file#*/}:删掉第一个 /及其左边的字符串:dir ...
- LeetCode(283)Move Zeroes
题目 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...
- AD采样求平均STM32实现
iADC_read(, &u16NTC_1_Sample_Val_ARR[]); == ui8FirstSampleFlag) { ; i<; i++) { u16NTC_1_Sampl ...
- LA 7056 Colorful Toy Polya定理
题意: 平面上给出一个\(N\)个点\(M\)条边的无向图,要用\(C\)种颜色去给每个顶点染色. 如果一种染色方案可以旋转得到另一种染色方案,那么说明这两种染色方案是等价的. 求所有染色方案数 \( ...
- BugBash活动分享
此文已由作者夏君授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. BugBash源至微软概念,翻译为<缺陷大扫除>,顾名思义是集中大家力量全面清扫Bug,确保产品质 ...
- sql获取指定表所有列名及注释
SELECT b.name as 字段名 ,Type_name(b.xusertype) as 字段类型, Isnull(c.VALUE,'') as 字段说明FROM sysobjects a jo ...
- python - 接口自动化测试 - contants - 常量封装
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: contants.py @ide: PyCharm Com ...
- python 时间、日期、时间戳的转换
在实际开发中经常遇到时间格式的转换,例如: 前端传递的时间格式是字符串格式,我们需要将其转换为时间戳,或者前台传递的时间格式和我们数据库中的格式不对应,我们需要对其进行转换才能与数据库的时间进行匹配等 ...