DFS和BFS的总结

如果我们使用 DFS/BFS 只是为了遍历一棵树、一张图上的所有结点的话,那么 DFS 和 BFS 的能力没什么差别,我们当然更倾向于更方便写、空间复杂度更低的 DFS 遍历。不过,某些使用场景是 DFS 做不到的,只能使用 BFS 遍历。这就是本文要介绍的两个场景:「层序遍历」、「最短路径」。还有写 DFS/BFS的算法模版。

1.DFS深度遍历

           递归模版:

void dfs(TreeNode root){
    if(root != null){
        return;
    }
    dfs(root.left);
    dfs(root.right);
}

       非递归模版:

void stackDfs(TreeNode root){
    stack stk;
    if(root != null){
        stk.push(root)
    }
    while(!stk.empty()){
        stack p = stk.top();
        visit(p);
        stk.pop();
        if(p.rchild) stk.push(p.rchild);//右边节点入栈
        if(p.lchild) stk.push(p.lchild);
    }

}

2.BFS广度遍历

void bfs(TreeNode root){
    Queue<TreeNode> queue = new ArrayDeque<>();
    if(root != null){
        queue.add(root);
    }
    while(!queue.isEmpty()){
        TreeNode node = queue.poll();
        if(node.left != null){
            queue.add(node.left);
        }
        if(node.right != null){
            queue.add(node.right);
        }
    }
 
}

    2.1  层次遍历

void bfs(TreeNode root){
    Queue<TreeNode> queue = new ArrayDeque<>();
    if(root != null){
        queue.add(root);
    }
    while(!queue.isEmpty()){
        int n = queue.size();  //把入队的节点数纪录下来,然后遍历这一层的节点就可以了
        for(int i = 0; i < n; i++){
            TreeNode node = queue.poll();
            if(node.left != null){
                queue.add(node.left);
            }
            if(node.right != null){
                queue.add(node.right);
            }
        } 
    }
}

      2.2  最短路径(无权)

     题目:给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。两个相邻元素间的距离为 1 。

class Solution {
    public int[][] updateMatrix(int[][] matrix) {
        Queue<int[]> queue = new LinkedList<>();
        int m = matrix.length;
        int n = matrix[0].length;
        // 初始化矩阵
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(matrix[i][j] == 0){
                    queue.offer(new int[] {i, j});
                }else{
                    matrix[i][j] = -1;
                }
            }
        }
        // 方向坐标
        int[] dx = new int[] {-1,1,0,0};
        int[] dy = new int[] {0,0,-1,1};
        while(!queue.isEmpty()){
            // 队首元素出队
            int[] point = queue.poll();
            //获取出队元素的x,y 坐标  
            int x = point[0];
            int y = point[1];
            //向上下左右扩展
            for(int i = 0; i < 4; i++){
                int newx = x + dx[i];
                int newy = y + dy[i];
                if(newx >= 0 && newx < m && newy >= 0 && newy < n && matrix[newx][newy] == -1 ){
                    matrix[newx][newy] = matrix[x][y] + 1;
                    queue.offer(new int[] {newx, newy});
                }
            }
        }
        return matrix;

    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值