22. Generate Parentheses Posted on 2024-12-19 Edited on 2025-02-12 In Leetcode 22. Generate Parentheses DescriptionGiven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example 1: 12Input: n = 3Output: ["((()))","(()())","(())()","()(())","()()()"] Example 2: 12Input: n = 1Output: ["()"] Constraints: 1 <= n <= 8 Hints/Notes 2024/12/18 backtracking 0x3Fâs solution(checked) SolutionLanguage: C++ 1234567891011121314151617181920212223242526272829303132class Solution {public: int n; vector<string> res; string path; vector<string> generateParenthesis(int n) { if (!n) { return {}; } this->n = n; path = string(n * 2, 0); dfs(0, 0); return res; } void dfs(int left, int right) { if (left == n && right == n) { res.push_back(path); return; } int index = left + right; if (left < n) { path[index] = '('; dfs(left + 1, right); } if (right < left) { path[index] = ')'; dfs(left, right + 1); } }};