void parenthesesDFS(int left,int right,string out,vector<string>& res)
{
if(left>right) return;
if(left==0&&right==0)
res.push_back(out);
else
{
if(left>0)
parenthesesDFS(left-1,right,out+'(',res);
if(right>0)
parenthesesDFS(left,right-1,out+')',res);
}
}
vector<string> generateParenthesis(int n) {
vector<string> res;
parenthesesDFS(n,n,"",res);
return res;
}
DFS*2
大概懂了一点。函数参数需要level+输出+容器。函数中判断是否到根或应掉头。