Discription
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[ "((()))", "(()())", "(())()", "()(())", "()()()" ]
Solution
vector<string> generateParenthesis(int n) { //类似二叉树递归
vector<string> res;
addingpar(res, "", n, n);
return res;
}
void addingpar(vector<string> &res, string str, int left, int right){
if (0 == left && 0 == right){
res.push_back(str);
return;
}
if (left > 0)
addingpar(res, str + '(', left - 1, right);
if (left < right)
addingpar(res, str + ')', left, right - 1);
}