BFS와 DFS는
해설코드
class P{
int open, close;
P(int open, int close){
this.open = open;
this.close = close;
}
}
public int solution(int n){
int answer = 0;
// 열리는 괄호의 갯수, 닫히는 괄호의 갯수 추적
Stack<P> stack = new Stack<>();
// Stack 대신 Queue를 사용해도 답을 얻을 수 있음
stack.push(new P(0,0));
while(!stack.isEmpty()){
P p = stack.pop();
if(p.open > n) continue;
if(p.open < p.close) continue;
if(p.open + p.close == 2 * n) {
answer++;
continue;
}
stack.push(new P(p.open+1, p.close));
stack.push(new P(p.open, p.close+1));
}
return answer;
}