LeetCode常见题型——深度优先搜索

1. 算法思想

深度优先搜索(Depth-first Search, DFS)在搜索到一个新的节点时,立即对该新节点进行往下遍历, 因此遍历需要用先入后出的栈来实现,也可通过与栈等价的递归实现。

对于树结构而言,由于总是对新节点调用遍历,因此看起来时向着更深的方向前进。

DFS可以用来检测环路:再次遍历到某节点,但父节点不同。

在DFS过程中,可以对搜索过的节点进行标记,以防止重复搜索,即状态记录或记忆化。

2. 常见题型

LeetCode-695. Max Area of Island [C++][Java]_贫道绝缘子的博客-CSDN博客You are given anm x nbinary matrixgrid. An island is a group of1's (representing land) connected4-directionally(horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.https://blog.csdn.net/qq_15711195/article/details/123146572?spm=1001.2014.3001.5502LeetCode-547. Number of Provinces [C++][Java]_贫道绝缘子的博客-CSDN博客There arencities. Some of them are connected, while some are not. If cityais connected directly with cityb, and citybis connected directly with cityc, then cityais connected indirectly with cityc.https://blog.csdn.net/qq_15711195/article/details/123165692LeetCode-417. Pacific Atlantic Water Flow [C++][Java]_贫道绝缘子的博客-CSDN博客There is anm x nrectangular island that borders both thePacific OceanandAtlantic Ocean. ThePacific Oceantouches the island's left and top edges, and theAtlantic Oceantouches the island's right and bottom edges.https://blog.csdn.net/qq_15711195/article/details/123170488

LeetCode-130. Surrounded Regions [C++][Java]_贫道绝缘子的博客-CSDN博客Given anm x nmatrixboardcontaining'X'and'O',capture all regions that are 4-directionallysurrounded by'X'. A region iscapturedby flipping all'O's into'X's in that surrounded region.https://blog.csdn.net/qq_15711195/article/details/123173977

LeetCode-257. Binary Tree Paths [C++][Java]_贫道绝缘子的博客-CSDN博客Given therootof a binary tree, returnall root-to-leaf paths inany order. Aleafis a node with no children.https://blog.csdn.net/qq_15711195/article/details/123311398

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值