publicList<String>generateParenthesis(int n){List<String> result =newLinkedList<String>();if (n >0) generateParenthesisCore("", n, n, result); return result;}privatevoidgenerateParenthesisCore(String prefix,int left,int right,List<String> result){if (left ==0&& right ==0) result.add(prefix);// Has left Parenthesis if (left >0) generateParenthesisCore(prefix+'(', left-1, right, result);// has more right Parenthesisif (left < right) generateParenthesisCore(prefix+')', left, right-1, result);}