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. Yii2 AR模型搜索数据条数不对,AR模型默认去重

    最近在做Yii2的项目时, 发现了一个yii2 自带的Ar模型会自动对搜索出来的字段去重. 默认去重字段: id,  其他字段暂没发现 1. 例如: public function fields { ...

  2. 科学计算库Numpy——概述

    Numpy主要用于数组的各种计算. 导入Numpy import numpy as np 数组类型 Numpy的数组类型为numpy.ndarray. array=np.array([1,2,3,4, ...

  3. Html5_标签

    HTML 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ****** - 数据库获取数据,然后替换到html文件的指定位置(W ...

  4. LeetCode(120) Triangle

    题目 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen ...

  5. light oj 1104 Birthday Paradox (概率题)

    Sometimes some mathematical results are hard to believe. One of the common problems is the birthday ...

  6. Linux学习-善用判断式

    利用 test 指令的测试功能 要检测系统上面某些文件或者是相关的属性时,利用 test 这个指令来工作真是好用得不 得了, 举例来说,我要检查 /dmtsai 是否存在时,使用: [dmtsai@s ...

  7. Linux学习-灾难复原的考虑

    硬件损毁,且具有完整备份的数据时 由于是硬件损毁,所以我们不需要考虑系统软件的不稳定问题,所以可以直接将完整的系统复原回去 即可. 由于软件的问题产生的被攻破资安事件 由于系统的损毁是因为被攻击,此时 ...

  8. Centos7 安装 OwnCloud 私有云

    OwnCloud 一款文件主机服务软件,就是我们平时使用的云存储,不过这是在自己主机的服务器上建立属于自己的私有云,OwnCloud 使用AGPLv3协议发布.本项目是基于PHP和SQLite,MyS ...

  9. NPM包的安装及卸载

    NPM全名:node package manager,是node包管理工具,负责安装.卸载.更新等.新版的NodeJS已经集成了npm.所以装好NodeJS的同时,npm也已经装好了! 可以用cmd命 ...

  10. JS实现——俄罗斯方块

    把以下代码保存成Tetris.html文件,使用Google或360浏览器打开 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 4.0 Transit ...