102. Binary Tree Level Order Traversal 广度优先遍历
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,直接暴力枚举
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);
}
}
这个的执行结果:
举例
队列情况如下
(初始状态)
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 广度优先遍历的更多相关文章
- 102. Binary Tree Level Order Traversal ------层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to righ ...
- [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, ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- 【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 ...
- [刷题] 102 Binary Tree Level Order Traversal
要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...
- 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, ...
- 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, ...
- 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...
- 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, ...
随机推荐
- ruby自动化之selenium webGUI
1.下载ruby语言包,windows下需要安装rubyinstall http://railsinstaller.org/en 2.cmd命令下安装selenium-webdriver gem包 g ...
- callback源码分析——callback_iter和callback
uvm_callback_iter,定义了function,first,last,next,prev的函数, 其中定义的还是相应uvm_callbacks的静态函数: 所以之前uvm_callback ...
- QT获取窗口句柄
winId()函数 SendMessage((HWND)(this->dlg->winId()),WM_SEND_MY_MESSAGE,0,0);
- django 设置不带后缀的访问路径
在urls.py 设置空路径,并指向对应的html文件 url(r'^$', views.index),
- sitecore系统教程之架构概述
Sitecore体验数据库(xDB)从实时大数据存储库中的所有通道源收集所有客户交互.它连接交互数据,为每个客户创建全面,统一的视图,并使营销人员可以使用数据来管理客户的实时体验. xDB架构非常灵活 ...
- Hibernate,关系映射的多对一单向关联、多对一双向关联、一对一主键关联、一对一外键关联、多对多关系关联
2018-11-10 22:27:02开始写 下图内容ORM.Hibernate介绍.hibername.cfg.xml结构: 下图内容hibernate映射文件结构介绍 下图内容hibernate ...
- いろはちゃんとマス目 / Iroha and a Grid (组合数学)
题目链接:http://abc042.contest.atcoder.jp/tasks/arc058_b Time limit : 2sec / Memory limit : 256MB Score ...
- kali linux下 hachcat安装
网上关于hachcat的简单使用方法介绍很多,然而却很少有在kali linux上的安装教程,找了好长时间,终于安装成功了,特此将中间借鉴的内容记录如下: #首先安装p7z,用于解压下载的p7z包 # ...
- Linux sed 命令字符串替换使用方法详解
1. sed替换的基本语法 sed 's/原字符串/替换字符串/' 单引号里面,s表示替换,三根斜线中间是替换的样式,特殊字符需要使用反斜线”\”进行转义.2. 单引号” ‘ ’”是没有办法用反斜线” ...
- Golang指针基本介绍及使用案例
一.指针的相关概念说明 变量:是基本类型,变量存的就是值,也叫值类型 地址:用于引用计算机的内存地址,可理解为内存地址的标签,通俗一点讲就是一间房在小区里的门牌号.如下图① 指针:指针变量存的是一个地 ...