BFS and DFS topic of LeetCode

BFS and DFS topic of LeetCode

BFS breadth-first , in accordance with the extended layer; the DFS depth first , is "one way to."

If it is a question of scale for the n-th power of 2, then the BFS time complexity is 2 ^ n, and DFS time complexity is n.

BFS is generally used to solve the shortest path problem maze , because the DFS may be coming to an end when it reaches the end of a large circle around.

BFS

1. a large space, is the exponential growth of

2. there will be no explosion risk stack space is application of heap space

3 can search for the shortest path or minimum

DFS

1. Space is proportional to the depth

2. The risk of explosion stack, such as under tree depth is too large

3. You can not search the shortest path of least

111. The minimum depth of a binary tree

Here Insert Picture Description

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(!root) return 0;
        int left = minDepth(root->left);
        int right = minDepth(root->right);
        if(!left&&right) return right+1; 
        if(!right&&left)  return left+1;
        return min(left,right)+1;
    }
};

279. perfect square number

Here Insert Picture Description

class Solution {
public:
    int numSquares(int n) {
        queue<int> q;
        vector<int> dist(n+1,INT_MAX);
        q.push(0);
        dist[0] = 0;
        while(q.size()){
            int t=q.front();
            q.pop();
            if(t==n) return dist[t];
            for(int i=1;i*i+t<=n;i++){
                int j=t+i*i;
                if(dist[j]>dist[t]+1){
                    dist[j]=dist[t]+1;
                    q.push(j);
                }
            }
        }
        return 0;
    }
};

733. The image rendering

Here Insert Picture Description

class Solution {
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        if(image.empty()||image[0].empty()) return image;
        int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
        int oldColor = image[sr][sc];
        if(oldColor==newColor) return image;
        image[sr][sc] = newColor;
        for(int i=0;i<4;i++){
            int x=sr+dx[i],y=sc+dy[i];
            if(x>=0&&x<image.size()&&y>=0&&y<image[0].size()&&image[x][y]==oldColor){
                floodFill(image,x,y,newColor);
            }
        }
        return image;
    }
};

200. The number of islands

Here Insert Picture Description

class Solution {
public:
    int m,n;
    int numIslands(vector<vector<char>>& grid) {
        if(grid.empty()||grid[0].empty())  return 0;
        n = grid.size(),m=grid[0].size();
        int res=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(grid[i][j]=='1'){
                    res++;
                    dfs(grid,i,j);
                }
            }
        }
        return res;
    }
    void dfs(vector<vector<char>>& grid,int x,int y){
        int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
        grid[x][y]='0';
        for(int i=0;i<4;i++){
           int a=x+dx[i],b=y+dy[i];
           if(a>=0 && a<n && b>=0 && b<m && grid[a][b]=='1')
                dfs(grid,a,b);
        }
    }
};
Published 28 original articles · won praise 34 · views 2685

Guess you like

Origin blog.csdn.net/weixin_44289697/article/details/104255628