leetcode广度_广度优先搜索-具有3个Leetcode示例的BFS图遍历指南

leetcode广度

Breadth First Search (BFS) is one of the most popular algorithms for searching or traversing a tree or graph data structure. In this tutorial, we will learn briefly how BFS works and explore a basic pattern that can be used to solve some medium and easy problems in Leetcode.

广度优先搜索(BFS)是用于搜索或遍历树或图数据结构的最受欢迎的算法之一。 在本教程中,我们将简要学习BFS的工作原理,并探索一种基本模式,该模式可用于解决Leetcode中的一些中等和简单问题。

Let's get started, shall we?

让我们开始吧,好吗?

So, we all know that a graph is a set of vertices and edges: G={V,E}. Traversing a graph means to visit every vertex and every edge exactly once in an orderly manner.

因此,我们都知道一个图是一组顶点和边:G = {V,E}。 遍历图形意味着以有序方式一次访问每个顶点和每个边缘。

In BFS, we are required to traverse the graph breadth-wise or level-wise. This means that we would first move horizontally and visit all the nodes of the current layer before moving on to the next layer.

在BFS中,我们需要遍历图的宽度或水平。 这意味着我们将首先水平移动并访问当前层的所有节点,然后再进入下一层。

Therefore, whenever we are asked to do some level order traversal, we can use the BFS technique.

因此,无论何时要求我们进行一些级别顺序遍历,都可以使用BFS技术。

In BFS, we would start traversing from 1 (the root node) and visit its child nodes 8, 5, and 2. We would store them in the order in which they were visited. This would allow us to visit the child nodes of 8 first (i.e. 6, 4 and 3), then of 5 (i.e. null), and then of 2 (i.e. 9) and so on.

在BFS中,我们将从1(根节点)开始遍历并访问其子节点8、5和2。我们将按照访问它们的顺序存储它们。 这将允许我们先访问8个子节点(即6、4和3),然后访问5个子节点(即null),然后访问2个子节点(即9),依此类推。

实作 (Implementation)

In order to implement BFS, a queue data structure is used. The queue stores the node and marks it as 'visited' until all its adjacent vertices are marked.

为了实现BFS,使用了队列数据结构。 队列存储该节点并将其标记为“已访问”,直到标记了其所有相邻顶点。

The queue follows the First In First Out (FIFO) method. This means that the neighbors of the node will be visited in the order in which they were inserted.

队列遵循先进先出(FIFO)方法。 这意味着将按照插入节点的顺序访问该节点的邻居。

BFS magic spell:

BFS魔法:

  1. Add a node to the queue

    将节点添加到队列
  2. Remove node

    删除节点
  3. Retrieve unvisited neighbors of the removed node, add them to queue

    检索已删除节点的未访问邻居,并将其添加到队列中
  4. Repeat steps 1, 2, and 3 as long as the queue is not empty.

    只要队列不为空,请重复步骤1、2和3。

Now let's look at some Leetcode problems and apply what we've learned.

现在,让我们看一下Leetcode的一些问题,并运用我们所学的知识。

102.二叉树级顺序遍历(难度:中) (102. Binary Tree Level Order Traversal (Difficulty: Medium) )

The question asks us to traverse through the graph and print the nodes at each level in a linked list. To solve this one, all we need to do is apply our magic spell!

这个问题要求我们遍历图形并在链表中打印每个级别的节点。 要解决这个问题,我们要做的就是应用我们的魔法!

Make sure you understand the code well, since this is the basic template we'll use to solve multiple problems. So let's go through it.

确保您对代码的理解很好,因为这是我们用来解决多个问题的基本模板 。 因此,让我们来看一下。

In the code above, we have at first inserted the root node in the queue. While the queue is not empty, we have removed this node from queue and inserted its left and right child in the queue.

在上面的代码中,我们首先将根节点插入了队列。 当队列不为空时,我们从队列中删除了该节点,并将其左右子节点插入队列。

But before that, we checked whether each of its children is null or not. If null, we would have gotten a Null Pointer Exception.

但是在此之前,我们检查了其每个子级是否为空。 如果为null,则将获得Null Pointer Exception。

The process is repeated again with the next elements that remains in the queue. The for loop is maintained to give us the list of nodes at each level in separate linked lists.

再次重复此过程,队列中保留下一个元素。 for循环 维护以在单独的链接列表中为我们提供每个级别的节点列表。

637.二叉树中的平均水平(难度:容易) (637. Average of Levels in a Binary Tree (Difficulty: Easy))

This question tells us to find the average value of nodes at each level of a binary tree in an array. This follows the same procedure as our previous problem with a bit of a tweak.

这个问题告诉我们找出数组中二叉树每个级别的节点的平均值。 进行一些调整后,遵循与先前问题相同的过程。

As you can see, all we did was copy and paste the template code. Then we simply put a sum variable within the for loop that can give us the sum of the node values at each level. This is what we will use to calculate our desired average.

如您所见,我们所做的就是复制并粘贴模板代码。 然后,我们简单地将一个sum变量放入for循环中,该变量可以为我们提供每个级别的节点值之和。 这就是我们将用来计算所需平均值的内容。

429. N元树级顺序遍历(难度:中) (429. N-ary Tree Level Order Traversal (Difficulty: Medium))

A tree in which each node has no more than N number of children is called a N-ary tree.

每个节点的子节点数量不超过 N个的树称为N元树。

This follows the exact same procedure as the traversal of a binary tree, except for the fact that in here, we insert all the children of a node in the queue. Remember that while solving problems related to binary tree, we have only inserted the left and right children of any given node in the queue.

这遵循与遍历二叉树完全相同的过程,不同的是在这里,我们将节点的所有子级插入队列中。 请记住,在解决与二叉树有关的问题时,我们仅在队列中插入了任何给定节点的左右子节点。

That's all! I hope this has helped you understand BFS better and that you have enjoyed the tutorial. Please recommend this post if you think it may be useful for someone else!

就这样! 我希望这可以帮助您更好地了解BFS,并希望您喜欢本教程。 如果您认为此帖子可能对其他人有用,请推荐此帖子!

翻译自: https://www.freecodecamp.org/news/breadth-first-search-a-bfs-graph-traversal-guide-with-3-leetcodeexamples/

leetcode广度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值