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. 字符串流stringReader

    String info ="good good study day day up";StringReader stringReader = new StringReader(inf ...

  2. Centos 7 安装 sphinx2.2 (转)

    一.首先到Sphinx官网找到下载地址:http://sphinxsearch.com/downloads/release/如果你比较懒,好吧:http://sphinxsearch.com/file ...

  3. EmEditor

    姓 名:ttrar.com 序 列 号:DKAZQ-R9TYP-5SM2A-9Z8KD-3E2RK 免费版地址:https://zh-cn.emeditor.com/#download

  4. SQL之to_date()以及关于日期处理的详解

    日期例子: SELECT TO_DATE('2006-05-01 19:25:34', 'YYYY-MM-DD HH24:MI:SS') FROM DUAL SELECT TO_DATE('2006- ...

  5. Delphi处理TWebBrowser的Close事件

    当TWebBrowser控件访问的 Web 页上的脚本调用window.close方法时,TWebBrowser控件可能会在窗体中消失.我们的程序应该对此作出反应,否则再次访问TWebBrowser控 ...

  6. Spring AOP demo 和获取被CGLIB代理的对象

    本文分为两部分:1)给出Spring AOP的一个例子(会使用CGLIB代理):2)给出获取被CGLIB代理的原始对象. 1.Spring AOP Demo 这部分参考了博文(http://www.v ...

  7. mssqlservers数据嗅探

    SQL Server - 最佳实践 - 参数嗅探问题 转.   文章来自:https://yq.aliyun.com/articles/61767 先说我的问题,最近某个存储过程,暂定名字:sp_a ...

  8. 法门扫地僧总结vue面试题(部分来源网络)

    Front-End 前端开发工程师面试宝典!   (本文部分有转载,不定期更新!)             前言(README.md) 本仓库是我整理的前端常见面试题,大部分由我整理,其中个别部分参考 ...

  9. Win8系统本地连接显示为网络2

    Win8系统中,当改变了网络环境,本地连接就会被识别为网络2,网络3等: 如果在一个固定的网络环境中,需要修改此名称,可以打开注册表: [HKEY_LOCAL_MACHINE\SOFTWARE\Mic ...

  10. 3Ds Max 2014

    原文地址:https://blog.csdn.net/u011518678/article/details/50764835 1.3Ds Max 2014 的安装和激活 激活地址: https://j ...