考虑一颗搜索树,对搜索树的所有路径进行遍历得到结果。搜索树的每个分支考虑的是选取或者不选取当前元素
void combine(int n, int depth,vector<vector<int>>& result,vector<bool>& visit){
if (depth >= n){
vector<int> temp;
for (int i = 0; i < n; ++i){
if (visit[i])temp.push_back(i);
}
if (!temp.empty())
result.push_back(temp);
return;
}
visit[depth] = 0;
combine(n, depth+1, result, visit);
visit[depth] = 1;
combine(n, depth + 1, result, visit);
}
int main(){
int n = 3;
vector<bool> visit(n);
vector<vector<int>> result;
combine(n, 0, result, visit);
return 0;
}