Leetcode Problem 130. Surrounded Regions

130. Surrounded Regions

Leetcode Solutions

DFS (Depth-First Search) Approach

  1. Iterate over the border cells of the board.
  2. For each 'O' cell on the border, perform a DFS traversal to mark all connected 'O' cells as 'E'.
  3. After marking, iterate over the entire board.
  4. For each cell, if it is 'O', change it to 'X' (captured); if it is 'E', revert it to 'O' (escaped).
  5. The board is now updated with all captured regions filled with 'X'.
UML Thumbnail

BFS (Breadth-First Search) Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...