Nothing fancy, just use recursive.
1 class Solution { 2 public: 3 void getP(vector&result, string current, int left, int right) { 4 if (left == 0 && right == 0) { 5 result.push_back(current); 6 return; 7 } 8 if (left > 0) { 9 getP(result, current + '(', left - 1, right + 1);10 }11 if (right > 0) {12 getP(result, current + ')', left, right - 1);13 }14 }15 vector generateParenthesis(int n) {16 vector result;17 getP(result, "", n, 0);18 return result;19 }20 };