1.DFS

1.1 【中等】最小基因变化(433)

image.png

  1. /**
  2. * 其实还是回溯法
  3. */
  4. public class MinMutation {
  5. int minStepCount = Integer.MAX_VALUE;
  6. public int minMutation(String start, String end, String[] bank) {
  7. dfs(new HashSet<>(), 0, start, end, bank);
  8. return (minStepCount == Integer.MAX_VALUE) ? -1 : minStepCount;
  9. }
  10. private void dfs(HashSet<String> step, int stepCount,
  11. String current, String end, String[] bank) {
  12. if (stepCount > minStepCount) {
  13. // 当前步骤大于最小次数直接返回
  14. return;
  15. }
  16. if (current.equals(end)) {
  17. // 判断是否是最少次
  18. minStepCount = Math.min(stepCount, minStepCount);
  19. }
  20. for (String str : bank) {
  21. int diff = 0;
  22. // 寻找与基因库不同的字符数
  23. for (int i = 0; i < str.length(); i++) {
  24. if (current.charAt(i) != str.charAt(i)) {
  25. if (++diff > 1) {
  26. break;
  27. }
  28. }
  29. }
  30. if (diff == 1 && !step.contains(str)) {
  31. // 回溯
  32. step.add(str);
  33. dfs(step, stepCount + 1, str, end, bank);
  34. step.remove(str);
  35. }
  36. }
  37. }
  38. }

1.2 【中等】在每个树行中找最大值(515)

image.png

  1. /**
  2. * 此题不是回溯法,但是用到了list的一些骚操作
  3. */
  4. public class LargestValues {
  5. /**
  6. * 保存对应层数的最大值,第一层的最大值在索引为0处,第二层在1处
  7. */
  8. private List<Integer> list = new ArrayList<>();
  9. public List<Integer> largestValues(TreeNode root) {
  10. dfs(root, 1);
  11. return list;
  12. }
  13. private void dfs(TreeNode node, int depth) {
  14. if (node == null) {
  15. return;
  16. }
  17. //如果当前层数还没有添加过值,就默认当前值是当前层数最大的值
  18. //如果当前层已经有最大值,就取出当前层最大值和当前值比较,如果当前值更大就更新对应层数的最大值
  19. if (depth > list.size()) {
  20. list.add(node.value);
  21. } else {
  22. if (node.value >= list.get(depth - 1)) {
  23. list.set(depth - 1, node.value);
  24. }
  25. }
  26. //继续递归左右子树
  27. dfs(node.left, depth + 1);
  28. dfs(node.right, depth + 1);
  29. }
  30. }

2.BFS

2.1 【中等】二叉树的层序遍历(102)

2.2 【中等】最小基因变化(433)

2.3 【中等】括号生成(22)

2.4 【中等】在每个树行中找最大值(515)

2.5 【中等】 二叉树的锯齿形层序遍历(103)

image.png

  1. /**
  2. * 和层序遍历相同,不过需要加一个判断条件,以决定是在头部添加还是尾部添加
  3. */
  4. public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
  5. List<List<Integer>> ans = new LinkedList<>();
  6. if (root == null) {
  7. return ans;
  8. }
  9. Queue<TreeNode> nodeQueue = new LinkedList<>();
  10. nodeQueue.offer(root);
  11. boolean isOrderLeft = true;
  12. while (!nodeQueue.isEmpty()) {
  13. Deque<Integer> levelList = new LinkedList<>();
  14. // 这边一定要单独写个size,不然会在循环中改变长度,不符合预期
  15. int size = nodeQueue.size();
  16. for (int i = 0; i < size; ++i) {
  17. TreeNode curNode = nodeQueue.poll();
  18. if (isOrderLeft) {
  19. levelList.offerLast(curNode.value);
  20. } else {
  21. levelList.offerFirst(curNode.value);
  22. }
  23. if (curNode.left != null) {
  24. nodeQueue.offer(curNode.left);
  25. }
  26. if (curNode.right != null) {
  27. nodeQueue.offer(curNode.right);
  28. }
  29. }
  30. ans.add(new LinkedList<>(levelList));
  31. isOrderLeft = !isOrderLeft;
  32. }
  33. return ans;
  34. }

2.6 【中等】 岛屿数量(200)

image.png
bfs

  1. /**
  2. * bfs
  3. */
  4. public int numIslands(char[][] grid) {
  5. if (grid == null || grid.length == 0) {
  6. return 0;
  7. }
  8. int nr = grid.length;
  9. int nc = grid[0].length;
  10. int numIslands = 0;
  11. for (int r = 0; r < nr; ++r) {
  12. for (int c = 0; c < nc; ++c) {
  13. if (grid[r][c] == '1') {
  14. ++numIslands;
  15. grid[r][c] = '0';
  16. // bfs
  17. Queue<Integer> neighbors = new LinkedList<>();
  18. neighbors.add(r * nc + c);
  19. while (!neighbors.isEmpty()) {
  20. // 将上下左右如果是1都置为0
  21. int id = neighbors.remove();
  22. int row = id / nc;
  23. int col = id % nc;
  24. if (row - 1 >= 0 && grid[row - 1][col] == '1') {
  25. neighbors.add((row - 1) * nc + col);
  26. grid[row - 1][col] = '0';
  27. }
  28. if (row + 1 < nr && grid[row + 1][col] == '1') {
  29. neighbors.add((row + 1) * nc + col);
  30. grid[row + 1][col] = '0';
  31. }
  32. if (col - 1 >= 0 && grid[row][col - 1] == '1') {
  33. neighbors.add(row * nc + col - 1);
  34. grid[row][col - 1] = '0';
  35. }
  36. if (col + 1 < nc && grid[row][col + 1] == '1') {
  37. neighbors.add(row * nc + col + 1);
  38. grid[row][col + 1] = '0';
  39. }
  40. }
  41. }
  42. }
  43. }
  44. return numIslands;
  45. }

dfs

  1. /**
  2. * dfs 核心思想还是将是1的上下左右置为0
  3. */
  4. class Solution {
  5. void dfs(char[][] grid, int r, int c) {
  6. int nr = grid.length;
  7. int nc = grid[0].length;
  8. if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') {
  9. return;
  10. }
  11. grid[r][c] = '0';
  12. dfs(grid, r - 1, c);
  13. dfs(grid, r + 1, c);
  14. dfs(grid, r, c - 1);
  15. dfs(grid, r, c + 1);
  16. }
  17. public int numIslands(char[][] grid) {
  18. if (grid == null || grid.length == 0) {
  19. return 0;
  20. }
  21. int nr = grid.length;
  22. int nc = grid[0].length;
  23. int num_islands = 0;
  24. for (int r = 0; r < nr; ++r) {
  25. for (int c = 0; c < nc; ++c) {
  26. if (grid[r][c] == '1') {
  27. ++num_islands;
  28. dfs(grid, r, c);
  29. }
  30. }
  31. }
  32. return num_islands;
  33. }
  34. }