DFS랑 BFS

jinvicky·2025년 4월 11일

Algorithm - Java

목록 보기
67/68

문제 감 잡는 데 들이는 시간도 충분해야 한다.

DFS

  • rc를 이용해서 4방향으로 재귀탐색을 진행한다.
  • drdc재귀호출에서 +1, -1하거나 방향을 배열로 정의해놓고 방향의 개수만큼 반복문을 호출할 수 있다.

DFS 쉬운 문제

✅ DFS 쉬운 문제 추천 (LeetCode Easy)

  1. Number of Islands
    1로 연결된 섬을 모두 세는 DFS 대표 문제
    🔗 https://leetcode.com/problems/number-of-islands/

  2. Flood Fill
    상하좌우로 연결된 같은 색 영역을 DFS로 색칠
    🔗 https://leetcode.com/problems/flood-fill/

  3. Max Area of Island
    DFS로 연결된 섬 중 가장 큰 면적 구하기
    🔗 https://leetcode.com/problems/max-area-of-island/

  4. Path Sum
    이진 트리에서 루트~리프 경로의 합이 target과 같은지
    🔗 https://leetcode.com/problems/path-sum/

  5. All Paths From Source to Target
    DAG(방향성 비순환 그래프)에서 시작~도착까지 모든 경로 탐색
    🔗 https://leetcode.com/problems/all-paths-from-source-to-target/

BFS

큐를 사용해서 레벨별로 가까운 노드부터 방문한다.

     0
    / \
   1   2
  /     \
 3       4
  1. 시작: queue = [0]

  2. 0 방문 → 1, 2 큐에 추가 → queue = [1, 2]

  3. 1 방문 → 3 큐에 추가 → queue = [2, 3]

  4. 2 방문 → 4 큐에 추가 → queue = [3, 4]

  5. 3 방문

  6. 4 방문

✅ BFS 쉬운 문제

Matrix
🔗 https://leetcode.com/problems/01-matrix/

Rotting Oranges
🔗 https://leetcode.com/problems/rotting-oranges/

Shortest Path in Binary Matrix
🔗 https://leetcode.com/problems/shortest-path-in-binary-matrix/

Number of Islands
🔗 https://leetcode.com/problems/number-of-islands/

Nearest Exit from Entrance in Maze
🔗 https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/

profile
개발, 그림, 기록

0개의 댓글