public class Solution {
    /**
     * 
     * @param n int整型 
     * @return string字符串ArrayList
     */
    /**
     * "((()))", "(()())", "(())()", "()()()", "()(())"
     * @param n
     * @return
     */
    public ArrayList<String> generateParenthesis (int n) {
        // write code here
        ArrayList<String> res = new ArrayList<>();
        if (n== 0){
            return  res;
        }
        recur("",n,n,res,n);
        return  res;
    }

    public  void recur(String str,int left,int right,ArrayList<String> res,int n){
       if(left == 0 && right == 0){
                res.add(str);
                return ;
           }
        if (left>right){
            return;
        }
        
        if (left>0){
            recur(str+"(",left-1,right,res,n);
        }
        if (right>0){
            
            recur(str+")",left,right-1,res,n);
        }
        

    }
    
    
}