0022.Generate Parentheses
class Solution {
public:
vector<string> result;
string path;
void backtracking(int left, int right, int n) {
if(left > n || right > n) return;
if(left == n && right == n) {
result.push_back(path);
return;
}
if(left >= right) {
path.push_back('(');
backtracking(left+1, right, n);
path.pop_back();
}
if(left > right) {
path.push_back(')');
backtracking(left, right+1, n);
path.pop_back();
}
}
vector<string> generateParenthesis(int n) {
path = "(";
backtracking(1, 0, n);
return result;
}
};