广度优先搜索练习一

例题一:N叉树得层序遍历1

给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。

树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。

示例 1:

输入:root = [1,null,3,2,4,null,5,6]
输出:[[1],[3,2,4],[5,6]]

层序遍历:广度优先

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ret = new ArrayList<>();
        Queue<Node> qu = new LinkedList<>();
        if(root==null)
            return ret;
        qu.offer(root);
        while(!qu.isEmpty()){
            int size = qu.size();
            List<Integer> list = new ArrayList<>();
            while(size-->0){
                Node node = qu.poll();
                list.add(node.val);
                for(Node child : node.children){
                    qu.offer(child);
                }
            }
            ret.add(list);
        }
        return ret;
    }
}

例题二:腐烂的橘子

在给定的 m x n 网格 grid 中,每个单元格可以有以下三个值之一:

  • 值 0 代表空单元格;

  • 值 1 代表新鲜橘子;

  • 值 2 代表腐烂的橘子。

每分钟,腐烂的橘子 周围 4 个方向上相邻 的新鲜橘子都会腐烂。

返回 直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 -1 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotting-oranges

示例一:

输入:grid = [[2,1,1],[1,1,0],[0,1,1]]
输出:4

示例二:

输入:grid = [[2,1,1],[0,1,1],[1,0,1]]
输出:-1
解释:左下角的橘子(第 2 行, 第 0 列)永远不会腐烂,因为腐烂只会发生在 4 个正向上。
class Solution {
    class Pair{
        int x;
        int y;
        public Pair(int x,int y){
            this.x = x;
            this.y = y;
        }
    }
    public int orangesRotting(int[][] grid) {
        Queue<Pair> qu = new LinkedList<>();
        int m = grid.length;
        int n = grid[0].length;
        //找烂橘子
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j]==2)
                    qu.offer(new Pair(i,j));
            }
        }
        
        int time = 0;
        int[][] dir = {{1,0},{-1,0},{0,1},{0,-1}};
        while(!qu.isEmpty()){
            //每一轮传染体
            int size = qu.size();
            //是否发生腐烂传染
            boolean isRot = false;
            while(size-->0){
                Pair pair = qu.poll();
                for(int[] a:dir){
                    int newx = pair.x+a[0];
                    int newy = pair.y+a[1];
                    if(newx<0||newx>=m || newy<0||newy>=n)
                        continue;
                    if(grid[newx][newy]==1){
                        isRot = true;
                        grid[newx][newy]=2;
                        qu.offer(new Pair(newx,newy));
                    }
                }
            }
            //发生时间加一;(z)
            if(isRot)
                time++;
        }
        //是否完全腐烂
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j]==1)
                    return -1;
            }
        }
        return time;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值