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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

#### 递归方法

```class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
return res;
}
void addingpar(vector<string> &v, string str, int n, int m){
if(n==0 && m==0) {
v.push_back(str);
return;
}
if(m > 0){ addingpar(v, str+")", n, m-1); }
if(n > 0){ addingpar(v, str+"(", n-1, m+1); }
}
};
```

``````Runtime: 8 ms, faster than 51.24% of C++ online submissions for Generate Parentheses.
Memory Usage: 15 MB, less than 80.17% of C++ online submissions for Generate Parentheses.``````

#### DFS

``````vector<string> generateParenthesis(int n) {
vector<string> res;
dfs("", n, n, res);
return res;
}
void dfs(string s, int opens, int closes, vector<string> &res) {
if(!opens && !closes) res.push_back(s);
if(opens) dfs(s + '(', opens - 1, closes, res);
if(opens < closes) dfs(s + ')', opens, closes - 1, res);
}``````

DFS解法相对于递归方法难以理解，可以观察当n等于1 2 3的时候程序执行过程。