# leetcode 22. Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

n：n对儿括号，写一个函数，生成所有括号的组合方式

For example, given n = 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"

1.想到二叉树，想到递归实现。
2.递归函数的参数形式要考虑清楚才可以。

class Solution {
public:
vector<string> generateParenthesis(int n)
{
//最后返回的
vector<string> ret;
if(n==0)
return ret;

//调用该函数求出 ret
dfs(ret,"",n,n);
return ret;
}

void dfs( vector<string> &ret , string tem , int l , int r)//记着要传ret的引用
{
if(l==0 && r==0)
{
ret.push_back(tem);
return ;
}

if(l>0)
{

dfs(ret , tem +"(", l-1 , r);
}

if(l<r)
{

dfs(ret , tem +")", l , r-1);
}
}
};


#### LeetCode --- 22. Generate Parentheses

2015-01-31 13:08:39

#### LeetCode 22. Generate Parentheses（生成括号）

2016-05-19 07:18:56

#### [LeetCode] 22. Generate Parentheses

2016-12-15 16:02:54

#### LeetCode *** 22. Generate Parentheses

2016-04-11 20:44:03

#### [LeetCode] 22. Generate Parentheses

2016-07-18 14:58:58

#### 22. Generate Parentheses LeetCode

2016-02-14 18:24:43

#### Leetcode -- 22. Generate Parentheses

2017-04-18 09:33:53

#### 【LeetCode】22. Generate Parentheses

2017-02-28 11:29:10

#### 【leetcode】22. Generate Parentheses

2015-11-03 19:49:37

#### [LeetCode]22. Generate Parentheses

2016-03-09 11:46:28