Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
public class Solution {
public List<String> generateParenthesis(int n) {
List<String> result=new LinkedList<String>();
if(n<=0) return result;
String s=new String();
dfs(0,0,n,s,result);
return result;
}
public void dfs(int left,int right,int n,String s,List<String> result){
if(left==n && left==right){
result.add(s);
return;
}
if(left<n) dfs(left+1,right,n,s+"(",result);
if(right<left) {
dfs(left,right+1,n,s+")",result);
}
}
}