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]
]

方案1,直接暴力枚举

Runtime: 280 ms, faster than 53.25% of C# online submissions for Binary Tree Level Order Traversal.
Memory Usage: 29.7 MB, less than 5.66% of C# online submissions for Binary Tree Level Order Traversal.
public class Solution {
private readonly List<Tuple<int, int>> _list = new List<Tuple<int, int>>(); private int _maxDepth; public IList<IList<int>> LevelOrder(TreeNode root)
{
IList<IList<int>> result = new List<IList<int>>();
GetAllNodes(root, );
for (int i = ; i <= _maxDepth; i++)
{
var temp = _list.Where(x => x.Item1 == i).Select(y => y.Item2).ToList();
result.Add(temp);
} return result;
} private void GetAllNodes(TreeNode node, int depth)
{
if (node == null)
return;
depth++;
if (_maxDepth < depth)
{
_maxDepth = depth;
}
_list.Add(new Tuple<int, int>(depth, node.val));
GetAllNodes(node.left, depth);
GetAllNodes(node.right, depth);
} private void WriteTreeNode(TreeNode node)
{
if (node == null)
{
Console.WriteLine("node is null");
return;
}
Console.Write($"node is {node.val}");
if (node.left == null)
{
Console.Write(", node.left is null");
}
else
{
Console.Write($", node.left is {node.left.val}");
}
if (node.right == null)
{
Console.WriteLine(", node.right is null");
}
else
{
Console.WriteLine($", node.right is {node.right.val}");
}
}
}

方案2,通过队列,在不同的depth中间插入null

https://stackoverflow.com/questions/31247634/how-to-keep-track-of-depth-in-breadth-first-search

You don't need to use extra queue or do any complicated calculation to achieve what you want to do. This idea is very simple.

This does not use any extra space other than queue used for BFS.

The idea I am going to use is to add null at the end of each level. So the number of nulls you encountered +1 is the depth you are at. (of course after termination it is just level).

 public IList<IList<int>> LevelOrder(TreeNode root)
{
Queue<TreeNode> queue = new Queue<TreeNode>(); IList<int> list = new List<int>();
IList<IList<int>> result = new List<IList<int>>(); if (root != null)
{
result.Add(list);
queue.Enqueue(root);
queue.Enqueue(null);
} while (queue.Count > )
{
var node = queue.Dequeue(); if (node == null)
{
queue.Enqueue(null);
if (queue.Peek() == null)
{
//You are encountering two consecutive `nulls` means, you visited all the nodes.
break;
}
else
{
list = new List<int>();
result.Add(list);
continue;
}
}
else
{
list.Add(node.val);
} Enqueue(queue, node.left);
Enqueue(queue, node.right);
} return result;
} private void Enqueue(Queue<TreeNode> tempQueue, TreeNode node)
{
if (node != null)
{
tempQueue.Enqueue(node);
}
}

这个的执行结果:

Runtime: 252 ms, faster than 86.41% of C# online submissions for Binary Tree Level Order Traversal.
Memory Usage: 29.1 MB, less than 87.74% of C# online submissions forBinary Tree Level Order Traversal.

举例

队列情况如下

(初始状态)

1 null1

(1出队列,1的左右子结点进队列)

null1  2  3

(null1出队列,说明depth=1遍历结束。新的null2进队列,depth=2结束的标志位,同时continue)

2   3 null2

(2出队列,2的左右子结点进队列)

3  null2  4  5

(3出队列,3的左右子结点入队列,因为3没有子结点,所以没有新进入queue的结点)

null2 4  5

(null2出队列,说明depth=2遍历结束。null3进队列,标记着depth=3结束的标志位,同时continue)

4  5 null3

(4出队列,4的左右子结点入队列,因为4没有子结点,所有没有新进入queue的结点)

5 null3

(5出队列,5的左右子结点入队列,因为5没有子结点,所有没有新进入queue的结点)

null3

(null3出队列,说明depth=3的遍历结束。null4进队列,标记着depth=4结束的标志位。这个时候queue.peek()=null4,连续2个null,意味着遍历结束,直接跳出循环)

102. Binary Tree Level Order Traversal 广度优先遍历的更多相关文章

  1. 102. Binary Tree Level Order Traversal ------层序遍历

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

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

  3. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

  4. 【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 ...

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

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

  6. 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, ...

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

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

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

  9. 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, ...

随机推荐

  1. UVa-116 Unidirectional TSP 单向旅行商

    题目 https://vjudge.net/problem/uva-116 分析 设d[i][j]为从(i,j)到最后一列的最小开销,则d[i][j]=a[i][j]+max(d[i+1][j+1], ...

  2. 2-2:python之控制结构

    一.程序流程图 1.用规定的一系列图形.流程线和文字说明算法从开始到结束全部步骤,包括基本操作和控制流程.2.流程图的基本元素包括: 1)  表示相应操作的框 2) 带箭头的流程线 3) 框内必要的文 ...

  3. 关于CTeX的几个大坑

    https://blog.csdn.net/zjutczj/article/details/53463478 最近一直忙着写小论文,毕业设计中期答辩,没有更新博客,忙过这一阵我会把这段时间学习机器学习 ...

  4. Linux——CentOS7安装gcc编译器详解

    使用yum安装gcc 使用yum命令安装还是非常easy的. yum -y install gcc gcc-c++ kernel-devel //安装gcc.c++编译器以及内核文件 手动安装gcc ...

  5. QtCreator 调试源码

    [1]安装源码 声明:要想调试进入Qt源码,必须首先保证我们安装了Qt源码.下面说明安装Qt源码注意事项. 一般安装过程(默认不安装源码): 安装源码过程(需要自己设置,点击“全选”): 综上所述:Q ...

  6. linux文件系统的用户和权限管理

    1. 为什么要有用户的概念? 多用户,多任务业务对系统资源的隔离产生需求 2. linux 用户的分类? 2.1. 管理员 拥有操作所有文件的权限 2.2. 普通用户 2.2.1. 普通登录用户 2. ...

  7. 对SQLite 数据库的一点点了解

    SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它的设计目标是嵌入式的,它占用资源非常低,在嵌入式设备中,可能只需要几百k的内存就够了. SQLit ...

  8. Struts2输入校验(编码方式)

    struts2对用户输入数据的校验方法有两种方式,一种是通过编码的方式,另一种则是通过使用XML配置方式. 本章主要介绍struts2编码方式的输入校验.以下将结合一个实例程序进行说明. 代码结构: ...

  9. linux yum配置本地iso镜像

    1.本地源配置:cdiso.repo 将iso镜像文件中所有内容复制到/public/software/cdrom 中,节点将本地yum指向此处. [root@node19 ~]# vim /etc/ ...

  10. The Little Prince-12/08

    The Little Prince-12/08 今天来点中文的经典语录+内心独白好不好呢? 狐狸说:“对我来说,你只是一个小男孩,就像其他成千上万个小男孩一样没有什么两样.我不需要你.你也不需要我.对 ...