给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

例如:

给定二叉树: [3,9,20,null,null,15,7],

3 / \ 9 20 / \ 15 7

返回其层次遍历结果:

[ [3], [9,20], [15,7] ]

class Solution {
public:
vector<vector<int> > levelOrder(TreeNode* root)
{
vector<vector<int> > res;
if(root == NULL)
return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
int size = q.size();
vector<int> temp;
for(int i = 0; i < size; i++)
{
TreeNode *node = q.front();
q.pop();
temp.push_back(node ->val);
if(node ->left)
q.push(node ->left);
if(node ->right)
q.push(node ->right);
}
res.push_back(temp);
}
return res;
}
};

Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历的更多相关文章

  1. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

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

  2. 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)

    这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...

  3. 102 Binary Tree Level Order Traversal 二叉树的层次遍历

    给定一个二叉树,返回其按层次遍历的节点值. (即zhu'ceng'de,从左到右访问).例如:给定二叉树: [3,9,20,null,null,15,7],    3   / \  9  20    ...

  4. leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历

    基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行 ...

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

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

  6. 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...

  7. LeetCode 102. Binary Tree Level Order Traversal02. 二叉树的层次遍历 (C++)

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

  8. 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树

    题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...

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

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

随机推荐

  1. neo4j中cypher语句多个模糊查询

    总结一下经验: neo4j中,cypher语句的模糊查询,好像是个正则表达式结构. 对于一个属性的多个模糊查询,可以使用如下写法: 比如,查询N类型中,属性attr包含'a1'或者'a2'的所有节点. ...

  2. linux 文件类型的颜色

    linux文件颜色的含义:蓝色代表目录 绿色代表可执行文件 红色表示压缩文件 浅蓝色表示链接文件 灰色表示其他文件 红色闪烁表示链接的文件有问题了 黄色表示设备文件 蓝色文件----------目录 ...

  3. 关于jar包启动遇到的问题

    一.找不到propertites文件,错误如下 原因是打成的jar不包含classpath信息,需要运行时指定,命令为 -Xbootclasspath/a: 后缀在核心class搜索路径后面.常用! ...

  4. JS规则 是非颠倒(逻辑非操作符)"!"是逻辑非操作符,也就是"不是"的意思,非真即假,非假即真

    是非颠倒(逻辑非操作符) "!"是逻辑非操作符,也就是"不是"的意思,非真即假,非假即真.好比小华今天买了一个杯子,小明说:"杯子是白色的" ...

  5. layui+croppers完成图片剪切上传

    不多说直接上代码: 前台代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" / ...

  6. html--垂直居中

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. Intel RealSense Depth Camera D435安装ROS 驱动——Ubuntu16.04

    官方教程 软件包下载链接 https://github.com/IntelRealSense/realsense-ros Download/Clone librealsense github repo ...

  8. PDO连续query()失败问题

    设置了非缓冲查询(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY)以后,连续query会导致下一次结果为false $conn = "mysql:host=127.0. ...

  9. Java-Shiro:目录

    ylbtech-Java-Shiro:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://yl ...

  10. 给自己立下一个flag先

    恩,今天开始写博客. 其实主要原因是被人甩了,想找个事情让自己忙起来. 主要原因是“男人没钱就是无能”,我是个应届毕业生.所以你懂的. 我不喜欢让心情不爽,所以只能找事情让自己忙起来.所以我开始立Fl ...