77. 组合
思路:回溯法解决的问题都可以抽象为树形结构(N叉树),用树形结构来理解回溯就容易多了
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
void backtracking(int n,int k,int startindex){
if(path.size()==k){
result.push_back(path);
return;
}
for(int i=startindex;i<=n;i++){
path.push_back(i);
backtracking(n,k,i+1);
path.pop_back();
}
}
vector<vector<int>> combine(int n, int k) {
backtracking(n,k,1);
return result;
}
};