[JavaScript] DFS를 구현해보자

공태윤·2024년 10월 10일
0

코딩테스트

목록 보기
7/9
const dfs = (here) => {
  visited[here] = true
  
  adj[here].forEach(there => {
  	if (!visited[here]) dfs(there)
  })
}

또는 다음과 같이도 구현할 수 있다

const dfs = (here) => {
  if (visited[here]) return
  
  visited[here] = true
  adj[here].forEach(dfs)
}
profile
기록으로 성장하는 프론트엔드 개발자입니다!

0개의 댓글