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

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

  2. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

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

  4. LeetCode(32)-Binary Tree Level Order Traversal

    题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...

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

  6. 【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, ...

  7. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

  8. (二叉树 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 ...

  9. 【题解】【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, ...

  10. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

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

随机推荐

  1. [BZOJ] 1127: [POI2008]KUP

    似曾相识的感觉 考虑另一个判断问题,给定一个k,问这个k是否可行 存在矩形和\(sum>2k\),则该矩阵不对判定做出贡献 存在矩形和\(sum\in [k,2k]\),则我们找到了一个解 于是 ...

  2. nodejs开发过程中遇到的一些插件记录

    1.chalk Github:https://github.com/chalk/chalk   终端样式定制插件,可自定义输出日志的样式. 1.semver   管网:https://semver.o ...

  3. crond定时操作 crontab

    * * * * *  分别表示 分钟  小时  日  月  星期(0-6) 30 17,28,19 * * *  或 30 17-19 * * *  在每天的17-19小时半点时刻执行 30 8-18 ...

  4. 在linux下安装并运行scrapyd

    系统:centos7.4 安装scrapyd:pip isntall scrapyd 因为我腾讯云上是python2与python3并存的 所以我执行的命令是:pip3 isntall scrapyd ...

  5. 用\r做出进度条

    在做ftp作业的时候,需要做一个上传和下载的进度条,做的时候发现用\r很容易就能做出来 def show_progress(self, has, total): rate = float(has) / ...

  6. HDU - 1251 统计难题(Trie树)

    有很多单词(只有小写字母组成,不会有重复的单词出现) 要统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). 每个单词长度不会超过10. Trie树的模板题.这个题内存把控不好容易MLE. ...

  7. zoj 4056

    At 0 second, the LED light is initially off. After BaoBao presses the button 2 times, the LED light ...

  8. 大家好,我是一个JAVA初学者,想在这里记下自己学习过程中的点点滴滴,请多多关照

    大家好,我是一个JAVA初学者,想在这里记下自己学习JAVA的点点滴滴,请多多关照. 以前一直在QQ空间里记录的,但感觉有些麻烦,而且有些东西自己理解的并不完善甚至都不正确,现在开始在这里重新记录,从 ...

  9. C++ 指针的小知识

    看个小例子: char* fun1(){ char * p = (char*)malloc(100); p = "helloww"; return p;} void fun2(ch ...

  10. SVM 与 LR的异同

    LR & SVM 的区别 相同点 LR和SVM都是分类算法. 如果不考虑核函数,LR和SVM都是线性分类算法,也就是说他们的分类决策面都是线性的. LR和SVM都是监督学习算法. LR和SVM ...