@@ -0,0 +1,1712 @@
+/*%0A * Key point: deepth first strategy%0A *   I can't conceive this solution by myself. Refering from others.%0A * To-Do: making sure the java parameter passing mechanism%0A */%0A%0Apublic class Solution %7B%0A    public ArrayList%3CString%3E generateParenthesis(int n) %7B%0A        StringBuffer sb = new StringBuffer();%0A        sb.append(%22%22);%0A        return helper(n, 0, 0, sb);%0A    %7D%0A    %0A    //parameter: total pairs n, left parenthese left, right parenthese right, temp buffer temp%0A    public ArrayList%3CString%3E helper(int n, int left, int right, StringBuffer sb)%7B%0A        ArrayList%3CString%3E result = new ArrayList%3CString%3E();%0A        %0A        if(left %3E n %7C%7C right %3E n)%0A            return result;%0A        %0A        //Work has been done, adding current StringBuffer to result    %0A        if(left == n && right == n)%7B%0A            result.add(sb.toString());%0A            return result;%0A        %7D%0A        %0A        //left parenthese %22(%22 is not enough%0A        if(left %3C n)%7B%0A            %0A            //adding a %22(%22 to the end of string buffer sb%0A            StringBuffer temp = new StringBuffer(sb);%0A            temp.append(%22(%22);%0A            %0A            //call helper() recursively%0A            result.addAll( helper(n, left+1, right, temp) );%0A        %7D%0A        %0A        //only if right parenthese %22)%22 is less than right parenthese %22)%22%0A        //we can add a %22)%22; otherwise, we shouldn't add %22)%22.%0A        if(right %3C left)%7B%0A            %0A            //adding a %22)%22 to the end of string buffer sb%0A            StringBuffer temp = new StringBuffer(sb);%0A            temp.append(%22)%22);%0A            %0A            //call helper() recursively%0A            result.addAll( helper(n, left, right+1, temp) );%0A        %7D%0A        %0A        return result;%0A    %7D%0A%7D

Solution for question Generate Parentheses