算法与数据结构基础 - 深度优先搜索(DFS)

时间:2023-03-09 08:56:00
算法与数据结构基础 - 深度优先搜索(DFS)

DFS基础

深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历、嵌套关系处理、回溯等,可以用递归、堆栈(stack)实现DFS过程。

关于广度优先搜索(BFS)详见:算法与数据结构基础 - 广度优先搜索(BFS)

关于递归(Recursion)详见:算法与数据结构基础 - 递归(Recursion)

树的遍历

DFS常用于二叉树的遍历,关于二叉树详见:

算法与数据结构基础 - 二叉查找树(Binary Search Tree)

算法与数据结构基础 - 二叉树(Binary Tree)

相关LeetCode题:

559. Maximum Depth of N-ary Tree  题解

897. Increasing Order Search Tree  题解

872. Leaf-Similar Trees  题解

108. Convert Sorted Array to Binary Search Tree  题解

100. Same Tree  题解

257. Binary Tree Paths  题解

101. Symmetric Tree  题解

110. Balanced Binary Tree  题解

112. Path Sum  题解

111. Minimum Depth of Binary Tree  题解

979. Distribute Coins in Binary Tree  题解

366. Find Leaves of Binary Tree  题解

1123. Lowest Common Ancestor of Deepest Leaves  题解

1110. Delete Nodes And Return Forest  题解

1026. Maximum Difference Between Node and Ancestor  题解

515. Find Largest Value in Each Tree Row  题解

199. Binary Tree Right Side View  题解

1145. Binary Tree Coloring Game  题解

337. House Robber III  题解

863. All Nodes Distance K in Binary Tree  题解

114. Flatten Binary Tree to Linked List  题解

971. Flip Binary Tree To Match Preorder Traversal  题解

105. Construct Binary Tree from Preorder and Inorder Traversal  题解

109. Convert Sorted List to Binary Search Tree  题解

116. Populating Next Right Pointers in Each Node  题解

124. Binary Tree Maximum Path Sum  题解

99. Recover Binary Search Tree  题解

968. Binary Tree Cameras  题解

图的遍历

树可视作一类特殊的图,更一般地DFS用于图的遍历,可视化过程

关于图详见:算法与数据结构基础 - 图(Graph)

相关LeetCode题:

733. Flood Fill  题解

841. Keys and Rooms  题解

695. Max Area of Island  题解

531. Lonely Pixel I  题解

756. Pyramid Transition Matrix  题解

694. Number of Distinct Islands  题解

711. Number of Distinct Islands II  题解

638. Shopping Offers  题解

851. Loud and Rich  题解

802. Find Eventual Safe States  题解

785. Is Graph Bipartite?  题解

200. Number of Islands  题解

1034. Coloring A Border  题解

332. Reconstruct Itinerary  题解

329. Longest Increasing Path in a Matrix  题解

834. Sum of Distances in Tree  题解

499. The Maze III  题解

嵌套关系处理

DFS可用于形如 "3[a2[c]]" 存在嵌套关系的问题处理,例如 LeetCode题目394. Decode String:

    // 394. Decode String
string decode(string s,int& pos){
string res="";
int num=;
for(;pos<s.length();pos++){
if(s[pos]=='['){
string tmp=decode(s,++pos);
for(;num>;num--) res+=tmp;
}
else if(s[pos]>=''&&s[pos]<='')
num=num*+s[pos]-'';
else if(s[pos]==']') return res;
else
res+=s[pos];
}
return res;
}

以上通过DFS进入到最内层的 '[ ]',之后随着函数返回、由内向外层层展开。

相关LeetCode题:

394. Decode String  题解

690. Employee Importance  题解

339. Nested List Weight Sum  题解

1020. Number of Enclaves  题解

DFS与回溯

回溯(Backtracking)算法中选择一条路径并走到底的思路,正是DFS。DFS是构成回溯算法的一部分。

关于回溯详见:算法与数据结构基础 - 回溯(Backtracking)

相关LeetCode题:

494. Target Sum  题解

473. Matchsticks to Square  题解

980. Unique Paths III  题解

301. Remove Invalid Parentheses  题解

DFS与Memorization

和BFS过程一样,应用DFS时也有可能重复访问同一节点,这时可用Memorization记录哪些节点已经访问过,避免路径重复遍历。

相关LeetCode题:

851. Loud and Rich  题解

547. Friend Circles  题解

490. The Maze  题解

1059. All Paths from Source Lead to Destination  题解

934. Shortest Bridge  题解

489. Robot Room Cleaner  题解

753. Cracking the Safe  题解

749. Contain Virus  题解

514. Freedom Trail  题解

546. Remove Boxes  题解