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. 11.vim编辑器命令

    VI中的多行删除与复制   方法一: 单行删除,:1(待删除行)d 多行删除 ,:1,10d   方法二: 光标所在行,dd 光标所在行以下的N行,Ndd   方法1: 光标放到第6行, 输入:2yy ...

  2. leetcode999

    class Solution: def numRookCaptures(self, board: 'List[List[str]]') -> int: basei = 0 basej = 0 r ...

  3. delphi 调用Webservice 引入wsdl 报错 document empty

    delphi 调用Webservice 引入wsdl 报错 document empty 直接引入wsdl 地址报错 document empty 解决办法:在浏览器里保存为xml文件,然后在开发环境 ...

  4. Centos下lnmp正确iptables配置规则

    查看iptable运行状态 service iptables status 清除已有规则 iptables -Fiptables -Xiptables -Z 开放端口 #允许本地回环接口(即运行本机访 ...

  5. day17-函数装饰器

    一.什么是装饰器 装饰器可以让其他函数在不需要做任何代码改变的前提下,增加额外的功能,装饰器的返回值也是一个函数对象.在 Python 中,函数是第一类对象,也就是说,函数可以做为参数传递给另外一个函 ...

  6. APP-10-文字识别-票据识别

    1.Postman测试 2.参数 https://cloud.baidu.com/doc/OCR/OCR-API.html#.E9.80.9A.E7.94.A8.E7.A5.A8.E6.8D.AE.E ...

  7. 尚硅谷springboot学习20-web开发简介

    使用SpringBoot 1).创建SpringBoot应用,添加我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来 3).自己编写业 ...

  8. sql中case when语句的使用

    case when语句有两种格式:简单case函数和搜索case函数. --简单Case函数CASE sexWHEN '1' THEN '男'WHEN '2' THEN '女'ELSE '其他' EN ...

  9. mybatis 获取insert返回的主键

    在我们开发过程中,在插入数据到数据库时,很多时候都需要把其主键返回,这里就说一下mybatis是怎么获取的. 其中mysql和oracle是不同的做法,因为mysql本身就提供字段自增的属性,而ora ...

  10. 半精度浮点数取5bit指数位

    半精度浮点是指用16bit表示一个浮点数,最高1bit为符号位,中间5bit为指数a,低10bit为尾数b Value = (符号位)(1+b/1024)*(2^(a-16)) 程序很简单,用pyin ...