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