Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
class Solution {
public:
vector<string> generateParenthesis(int n)
{
vector<string> ret;
findAll(n,n,"",ret);
return ret;
}
void findAll(int left,int right,string out,vector<string> &ret)
{
if(left>right)
return;
if(left==0&&right==0)
return ret.push_back(out);
else
{
if(left>0)
findAll(left-1,right,out+'(',ret);
if(right>0)
findAll(left,right-1,out+')',ret);
}
}
};