1.优秀题解(dfs 实现全排列)
2.题解(包含递归搜索树)
充分必要条件只适用一类括号的情况
class Solution {
public:
vector<string> res;
vector<string> generateParenthesis(int n) {
dfs(n, 0, 0, "");
return res;
}
void dfs(int n, int lc, int rc, string seq) {
if (lc == n && rc == n) res.push_back(seq);
else {
if (lc < n) dfs(n, lc + 1, rc, seq + '('); //情况1
if (rc < n && lc > rc) dfs(n, lc, rc + 1, seq + ')'); //情况2
}
}
};