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,null,null,15,7],

3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]
思路:层次遍历用队列来实现,这里因为需要知道在哪一层,所以需要用两个队列/或者创建一个数据结构,包含TreeNode以及level信息。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<queue<TreeNode*>> q();
int curIndex = ;
int nextIndex = ;
vector<int> retItem;
vector<vector<int>> ret; if(root) q[curIndex].push(root);
while(!q[curIndex].empty()){
retItem.push_back(q[curIndex].front()->val);
if(q[curIndex].front()->left) q[nextIndex].push(q[curIndex].front()->left);
if(q[curIndex].front()->right) q[nextIndex].push(q[curIndex].front()->right);
q[curIndex].pop(); if(q[curIndex].empty()){ //end of this level
ret.push_back(retItem);
retItem.clear();
curIndex = (curIndex+) & 0x01;
nextIndex = (nextIndex+) & 0x01;
}
} return ret;
}
};

102. Binary Tree Level Order Traversal (Tree, Queue; BFS)的更多相关文章

  1. Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS

    题意是倒过来层次遍历二叉树 下面我介绍下BFS的基本框架,所有的BFS都是这样写的 struct Nodetype { int d;//层数即遍历深度 KeyType m;//相应的节点值 } que ...

  2. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

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

  4. LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8

    102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...

  5. 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】102. Binary Tree Level Order Traversal (2 solutions)

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  7. [刷题] 102 Binary Tree Level Order Traversal

    要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...

  8. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

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

  9. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

随机推荐

  1. Linux定时任务 结合PHP实现实时监控

    首先说说cron,它是一个linux下的定时执行工具.根用户以外的用户可以使用 crontab 工具来配置 cron 任务. 所有用户定义的 crontab 都被保存在/var/spool/cron ...

  2. Django从MySQL数据库生成model

    字段太多的话,手动建表,然后用 inspectdb 命令生成model文件,效率会高很多: inspectdb  表名 >> model文件名.py >> 是追加在文件末尾:& ...

  3. Office2019都有哪些强大功能

    前阵子是微软一年一度的Ignite大会.而其中最引人注目.也是与我们一般人最息息相关的消息,当然是Office 2019的正式发布. 尽管Office 2019所更新的功能,对于Office 365的 ...

  4. Firebird日期时间操作

    最近在使用Firebird数据做 一项目,使用FireBird边用边学.(以下转贴) 查询2007年度以后的,12月份以上的数据记录,datetime为timestamp字段 select * fro ...

  5. ReultSet有什么作用和使用

    结果集(ResultSet)是数据中查询结果返回的一种对象,可以说结果集是一个存储查询结果的对象,但是结果集并不仅仅具有存储的功能,他同时还具有操纵数据的功能,可能完成对数据的更新等. int col ...

  6. 创建一个Maven Web应用程序

    1 在Eclipes创建maven,首先File new ,在other 中找到Maven,Maven Project,下一步选择WebApp,创建Maven工程名字,完成 2 在新建的Maven工程 ...

  7. css美化select标签,兼容ie10 ie10+,chrome。但不支持ie9 ie9-

    让ie9 ie9+ 和非ie的浏览器加载这个hack.ie8,ie8- 就用自己的默认样式 <!-- email:416960428@qq.com author:李可 --> <!- ...

  8. Java模板引擎之freemarker简介

  9. CodeForces-1132C-Painting the Fence-(前缀和)

    You have a long fence which consists of nn sections. Unfortunately, it is not painted, so you decide ...

  10. vim字符串替换及小技巧

    vi/vim 中可以使用 :s 命令来替换字符串.以前只会使用一种格式来全文替换,今天发现该命令有很多种写法(vi 真是强大啊,还有很多需要学习),记录几种在此,方便以后查询. :s/vivian/s ...