回溯法模板
List<List<Integer>> res = new ArrayList<>();
//path表示的是存放的路径
void dfs( 一些题目所涉及的变量,List<Integer> path){
//终止递归的条件
if(){
//终止的时候,储存一些东西,如储存路径。
res.add(new ArrayList<>(path)); //res.add(path) 这种写法是错误的,结果会导致res中各个path的地址都是一样的,导致值也是一样的。
return;
}
dfs(u+1,path);
//如果是二叉树的题目,一般是dfs(root.left,path);dfs(root.right,path);
//恢复现场
path.remove(path.size()-1);
//如果有其他的需要恢复的变量也要恢复。
}