一、什么是深度优先搜索(DFS)?
三、三种深度优先搜索题型
1. 二叉树问题
二叉树的题型中有很大一部分是用DFS解决,下面是二叉树题型的归纳:
https://blog.csdn.net/Kejonho/article/details/111306097
2. 排列组合问题
使用DFS,在一个集合中得到所有满足条件的排列或者组合。模版如下:
public List<List> main(Array arr) {
List<List> result1 = new List<List>();
getCombination(arr, 0, new List() ,result1);
List<List> result2 = new List<List>();
getPermutation(arr, new Array(), new List(), result2)
}
private void getCombination(Array arr,
int startIndex,
List comb,
List<List> combs) {
//在把一个组合放入答案集时,记得做copy,因为comb在递归中会不断被改变
combs.add(