本题是广度优先遍历(BFS)实现树的层次遍历,使用队列实现。

class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode*>q;
if (root != NULL)
{
q.push(root);
while (!q.empty())
{
vector<int> tmp;
vector<TreeNode*> T;
while (!q.empty())
{
TreeNode* t = q.front();
q.pop();
tmp.push_back(t->val);
if (t->left != NULL)
{
T.push_back(t->left);
}
if (t->right != NULL)
{
T.push_back(t->right);
}
}
res.push_back(tmp);
for (auto x : T)
{
q.push(x);
}
}
}
return res;
}
};

补充一个python的实现:

 class Solution:
def lOrder(self,temp,result):
count = len(temp)
newary = []
while count > :
top = temp.pop()
newary.append(top.val)
count -=
if top.left != None:
temp.append(top.left)
if top.right != None:
temp.append(top.right)
if len(newary) > :
result.append(newary)
if len(temp) > :
self.lOrder(temp,result) def levelOrder(self, root: TreeNode) -> List[List[int]]:
result = []
temp = []
if root != None:
temp.append(root)
self.lOrder(temp,result)
return result

leetcode102的更多相关文章

  1. 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)

    从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...

  2. LeetCode102 Binary Tree Level Order Traversal Java

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

  3. [Swift]LeetCode102. 二叉树的层次遍历 | Binary Tree Level Order Traversal

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

  4. (二叉树 BFS) leetcode102. Binary Tree Level Order Traversal

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

  5. 【leetcode-102,107,103】 二叉树的层次遍历

    102. 二叉树的层次遍历 (1过,隐蔽错误花时间很多,简单题目本应很快,下次注意红色错误的地方) 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: ...

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

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

  7. leetcode-102.层序遍历二叉树(正序)· BTree

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

  8. LeetCode102. 二叉树的层次遍历

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

  9. Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历

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

随机推荐

  1. day 08文件与字符编码

    ASCII  一个字符占一个字节 GBK 中文使用两个字节,英文使用1个字节,使用开头一个比特位标识是英文还是中文 unicode:支持任何国家的语言,全部字符都是使用两个字节 utf-8 一个英文占 ...

  2. VS使用Nuget教程详解 Visual studio 安装第三方的组件库

    首先说明Nuget是什么呢?它的官方是:https://www.nuget.org/ 官方主页的介绍如下: What is NuGet? NuGet is the package manager fo ...

  3. Cannot find ./catalina.sh The file is absent or does not have execute permission This file is nee

    从tomcat官网上下载了apache-tomcat-5.5.36.zip,在window xp系统里面解压以后,直接放在了linux服务器上. 进入tomcat/bin目录,执行启动的时候出现如下错 ...

  4. linux日常命令之一

    zcat Linux中,cat命令可查看文本内容: 对于压缩包内的文本,可使用zcat命令,在不解压的情况下查看文本内容: iconv file -i 可查看文件字符集: iconv为字符集转换命令, ...

  5. 微信小程序传值以及获取值方法

    http://www.51xuediannao.com/xiaochengxu/xiaochengxu-chuanzhi.html

  6. C语言求矩阵的逆

    #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <window ...

  7. python:学习自顶向下程序设计:竞技体育模拟

    学习过程记录: 一,需求及框架: 二:程序代码: #sports.py from random import random def main(): #熟悉函数的调用 printInfo() probA ...

  8. 在html中做表格以及给表格设置高宽字体居中和表格线的粗细

    今天学习了如何用HTML在网页上做表格,对于我这种横列部分的属实有点麻烦,不过在看着表格合并单过格的时候我把整个表格看做代码就容易多了. 对于今天的作业让我学习了更多的代码,对于代码的应用希望更加熟练 ...

  9. 《DSP using MATLAB》Problem 7.10

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  10. Django 小饭桌项目实战笔记

    gulp-sass安装 安装报错,原因未设置全局镜像源npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ ...