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, ...
随机推荐
- linux命令目录
一.文件和目录.(文件目录的增删改查) ls pwd cd mkdir touch rmdir ln dd rm cp mv nl cat tac more less head tail stat # ...
- SVN—使用总结
SVN使用教程总结 为什么要使用SVN? 在程序的编写过程中,每个程序员都会负责开发一个或多个模块,且开发中会生成很多不同的版本, 这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版 ...
- Cocos Creator学习目录
目录 安装和启动 文件结构 编辑器基础 基本概念 (场景树 节点 坐标 组件 ) Cocos Creator 脚本简介 Cocos Creator调试 节点 cc.Node 组件开发cc.Compon ...
- [7] Windows内核情景分析---线程同步
基于同步对象的等待.唤醒机制: 一个线程可以等待一个对象或多个对象而进入等待状态(也叫睡眠状态),另一个线程可以触发那个等待对象,唤醒在那个对象上等待的所有线程. 一个线程可以等待一个对象或多个对象, ...
- 如何修改Xampp服务器上的mysql密码(图解)
https://www.jb51.net/article/111289.htm https://www.cnblogs.com/Leequik/p/5323795.html 1.点击MySQL的adm ...
- jQuery选择器--:selected和:checked
:selected 概述 匹配所有选中的option元素 <!DOCTYPE html> <html> <head> <meta charset=" ...
- Swift 了解(2)
循环(Loops) 1. For条件递增语句 ; counter < ; counter++ ) { liftWeights( ) } 语法是这样的:用for作为循环的开始,告诉Xcode你要声 ...
- 参与.net开源项目开发
EntityFramework6 https://github.com/aspnet/EntityFramework6 https://github.com/aspnet/EntityFramewor ...
- python: ImportError: cannot import name 'Style' from 'openpyxl.styles' 解决方法
import os, openpyxl from openpyxl.styles import Font, Style os.chdir("C:\\") wb = openpyxl ...
- K-Means算法的Java实现
K-means算法是硬聚类算法,是典型的基于原型的目标函数聚类方法的代表,它是数据点到原型的某种距离作为优化的目标函数,利用函数求极值的方法得到迭代运算的调整规则.K-means算法以欧式距离作为相似 ...