广度优先搜索
一、实验题目
102.二叉树的层序遍历
给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
面试题08.10.颜色填充
编写函数,实现许多图片编辑软件都支持的「颜色填充」功能。 待填充的图像用二维数组 image 表示,元素为初始颜色值。初始坐标点的行坐标为 sr 列坐标为 sc。需要填充的新颜色为 newColor 。 「周围区域」是指颜色相同且在上、下、左、右四个方向上存在相连情况的若干元素。 请用新颜色填充初始坐标点的周围区域,并返回填充后的图像。
LCR 150.
彩灯装饰记录2
一棵圣诞树记作根节点为 root 的二叉树,节点值为该位置装饰彩灯的颜色编号。请按照从左到右的顺序返回每一层彩灯编号,每一层的结果记录于一行。
765.情侣牵手
n 对情侣坐在连续排列的 2n 个座位上,想要牵到对方的手。 人和座位由一个整数数组 row 表示,其中 row[i] 是坐在第 i 个座位上的人的 ID。情侣们按顺序编号,第一对是 (0, 1),第二对是 (2, 3),以此类推,最后一对是 (2n-2, 2n-1)。 返回 最少交换座位的次数,以便每对情侣可以并肩坐在一起。 每次交换可选择任意两人,让他们站起来交换座位。
二、实验思路和方案
102.二叉树的层序遍历
题目要求的二叉树的从上至下打印(即按层打印),又称为二叉树的广度优先搜索。广度优先搜索通常借助队列的先入先出特性来实现。
将本层全部节点打印到一行,并将下一层全部节点加入队列,以此类推,即可分为多行打印。
面试题08.10.颜色填充
可以将与指定节点相邻的,且颜色相同,且不等于新颜色的节点加入队列Queue中,然后依次取出再进行处理。
LCR 150.彩灯装饰记录2
在二叉树层序遍历的基础上,这道题要求将每层打印到一行。因此我们可以考虑将当前全部节点打印到一行,并将下一层全部节点加入队列,以此类推,即可分为多行打印。
765.情侣牵手
首先将节点都标记为未访问,并遍历图中的每个节点。如果发现一个未访问的节点,就从该节点出发,沿着图中的边,将其余的未访问的节点都标记为已访问,并同时统计标记的次数。当遍历过程终止时,标记的数量次数即为连通分量的大小。
三、实验代码
102.二叉树的层序遍历
class Solution {public List<List<Integer>> levelOrder(TreeNode root) {Queue<TreeNode> queue = new LinkedList<>();List<List<Integer>> res = new ArrayList<>();if (root != null) queue.add(root);while (!queue.isEmpty()) {List<Integer> tmp = new ArrayList<>();for(int i = queue.size(); i > 0; i--) {TreeNode node = queue.poll();tmp.add(node.val);if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}res.add(tmp);}return res;}}面试题08.10.颜色填充
class Solution {public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {Queue<int[]> queue = new LinkedList<>();queue.offer(new int[]{sr, sc});int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};while (!queue.isEmpty()) {int[] arr = queue.poll();int i = arr[0];int j = arr[1];int oldColor = image[i][j];image[i][j] = newColor;for (int k = 0; k < dir.length; k++) {int r = dir[k][0] + i;int c = dir[k][1] + j;if (r >= 0 && r < image.length && c >= 0 && c < image[0].length && image[r][c] == oldColor && image[r][c] != newColor) {queue.offer(new int[]{r, c});}}}return image;}}LCR 150.彩灯装饰记录2
class Solution {public List<List<Integer>> decorateRecord(TreeNode root) {Queue<TreeNode> queue = new LinkedList<>();List<List<Integer>> res = new ArrayList<>();if (root != null) queue.add(root);while (!queue.isEmpty()) {List<Integer> tmp = new ArrayList<>();for (int i = queue.size(); i > 0; i--) {TreeNode node = queue.poll();tmp.add(node.val);if (node.left != null) queue.add(node.left);if (node.right != null) queue.add(node.right);}res.add(tmp);}return res;}}765.情侣牵手
class Solution {public int minSwapsCouples(int[] row) {int m = row.length;int n = m / 2;List<Integer>[] graph = new List[n];for (int i = 0; i < n; ++i) {graph[i] = new ArrayList<>();}for (int i = 0; i < m; i += 2) {int x = row[i] / 2, y = row[i + 1] / 2;if (x != y) {graph[x].add(y);graph[y].add(x);}}boolean[] visited = new boolean[n];int res = 0;for (int i = 0; i < n; ++i) {if (!visited[i]) {Queue<Integer> queue = new LinkedList<>();visited[i] = true;queue.offer(i);int cnt = 0;while (!queue.isEmpty()) {int x = queue.poll();++cnt;for (int y : graph[x]) {if (!visited[y]) {visited[y] = true;queue.offer(y);}}}res += cnt - 1;}}return res;}}四、实验结果及分析
102.二叉树的层序遍历

面试题08.10.颜色填充

LCR 150.彩灯装饰记录2

765.情侣牵手

五、实验总结及体会
通过本次实验,我对广度优先搜索有了初步的理解和认识。广度优先搜索适用于无权重图的最短路径和状态空间搜索,但在状态空间过大时可能会导致内存溢出。
