这里写目录标题
树和网格的DFS模板
二叉树的 DFS 有两个要素:「访问相邻结点」和「判断 base case」
访问相邻结点:递归访问左右子树
判断 base case:一般来说,二叉树遍历的 base case 是 root == null。这样一个条件判断其实有两个含义:一方面,这表示 root 指向的子树为空,不需要再往下遍历了。另一方面,在 root == null 的时候及时返回,可以让后面的 root.left 和 root.right 操作不会出现空指针异常
void traverse(TreeNode root) {
// 判断 base case
if (root == null) {
return;
}
// 访问两个相邻结点:左子结点、右子结点
traverse(root.left);
traverse(root.right);
}
网格的 DFS 有两个要素:「访问相邻结点」和「判断 base case」
访问相邻结点:四周
判断 base case:判断是否越界 判断是不是岛屿
void dfs(int[][] grid, int r, int c) {
// 判断 base case
if (!inArea(grid, r, c)) {
return;
}
// 如果这个格子不是岛屿,直接返回
if (grid[r][c] != 1) {
return;
}
grid[r][c] = 2; // 将格子标记为「已遍历过」
// 访问上、下、左、右四个相邻结点
dfs(grid, r - 1, c);
dfs(grid, r + 1, c);
dfs(grid, r, c - 1);
dfs(grid, r, c + 1);
}
// 判断坐标 (r, c) 是否在网格中
boolean inArea(int[][] grid, int r, int c) {
return 0 <= r && r < grid.length
&& 0 <= c && c < grid[0].length;
}
200. 岛屿数量
class Solution {
public int numIslands(char[][] grid) {
if(grid.length == 0 || grid[0].length == 0){
return 0;
}
int m = grid.length, n = grid[0].length;
boolean[][] visited = new boolean[m][n];
int count = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == '1' && !visited[i][j]){
dfs(grid, i, j, visited);
count++;
}
}
}
return count;
}
public void dfs(char[][] grid, int i, int j, boolean[][] visited){
if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || visited[i][j] || grid[i][j] == '0'){
return;
}
visited[i][j] = true;
dfs(grid, i+1, j, visited);
dfs(grid, i-1, j, visited);
dfs(grid, i, j+1, visited);
dfs(grid, i, j-1, visited);
}
}
直接原地修改数组,节省visited数组的空间复杂度。
class Solution {
public int numIslands(char[][] grid) {
if(grid.length == 0 || grid[0].length == 0) return 0;
int m = grid.length, n = grid[0].length;
int count = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j <n; j++){
if(grid[i][j] == '1'){
dfs(grid, i, j);
count++;
}
}
}
return count;
}
public void dfs(char[][] grid, int i, int j){
if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0'){
return;
}
grid[i][j] = '0';
dfs(grid, i+1, j);
dfs(grid, i-1, j);
dfs(grid, i, j+1);
dfs(grid, i, j-1);
}
}
695. 岛屿的最大面积
class Solution {
public int maxAreaOfIsland(int[][] grid) {
int area = 0;
int[][] visited = new int[grid.length][grid[0].length];
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[0].length; j++){
if(grid[i][j] == 1 && visited[i][j] == 0){
area = Math.max(area, dfs(grid, i, j, visited));
}
}
}
return area;
}
public int dfs(int[][] m, int i, int j, int[][] visited){
if (i < 0 || j < 0 || i == m.length || j == m[0].length || m[i][j] == 0 || visited[i][j] == 1) {
return 0;
}
visited[i][j] = 1;
int[] di = {1, -1, 0, 0};
int[] dj = {0, 0, 1, -1};
int ans = 1;
for(int index = 0; index < 4; index++){
int next_i = i + di[index], next_j = j + dj[index];
ans += dfs(m, next_i, next_j, visited);
}
return ans;
}
}
不使用visited数组,减少空间复杂度
class Solution {
public int maxAreaOfIsland(int[][] grid) {
if(grid.length == 0 || grid[0].length == 0){
return 0;
}
int area = 0;
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[0].length; j++){
if(grid[i][j] == 1){
// 求最大面积
area = Math.max(area, dfs(grid, i, j));
}
}
}
return area;
}
// 求一个岛屿的面积
public int dfs(int[][] grid, int i, int j){
if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length || grid[i][j] != 1){
return 0;
}
grid[i][j] = 2;
return 1
+ dfs(grid, i+1, j)
+ dfs(grid, i-1, j)
+ dfs(grid, i, j+1)
+ dfs(grid, i, j-1);
}
}