dfs 的一个关键是termination condition
public class Solution { public ArrayListgenerateParenthesis(int n) { ArrayList res = new ArrayList (); if(n<1) return res; dfs(n,n,res,new String()); return res; } private void dfs(int nl, int nr,ArrayList res, String item){ if(nr nl not working!!! if(nr==0 && nl==0) res.add(item); if(nl>0) dfs(nl-1, nr, res, item+'('); if(nr>0) dfs(nl, nr-1, res, item+')'); }}