牛客60(括号生成)
60.括号生成
import java.util.*; public class Solution { ArrayList<String> res = new ArrayList<>(); public ArrayList<String> generateParenthesis (int n) { traversal(0, 0, n, ""); return res; } public void traversal(int left, int right, int n, String temp) { if (left == n && right == n) { res.add(temp); return; } if (left < n) { traversal(left + 1, right, n, temp + "("); } if (right < n && right < left) { traversal(left, right+1, n, temp + ")"); } } }