Breadth First Search
598. Zombie in Matrix
https://www.lintcode.com/problem/zombie-in-matrix/description?_from=ladder&&fromId=1
class Coordinate { int x, y; public Coordinate(int x, int y) { this.x = x; this.y = y; } } public class Solution { /** * @param grid: a 2D integer grid * @return: an integer */ public int PEOPLE = 0; public int ZOMBIE = 1; public int WALL = 2; public int[] deltaX = {1, 0, -1, 0}; public int[] deltaY = {0, 1, 0, -1}; public int zombie(int[][] grid) { // write your code here if(grid == null || grid.length == 0 || grid[0].length == 0) { return 0; } int row = grid.length; int col = grid[0].length; Queue<Coordinate> queue = new LinkedList<>(); int people = 0; for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { if(grid[i][j] == PEOPLE) { people++; } else if(grid[i][j] == ZOMBIE) { queue.offer(new Coordinate(i, j)); } } } //corner case if(people == 0) { return 0; } int days = 0; while(!queue.isEmpty()) { days++; int size = queue.size(); for(int i = 0; i < size; i++) { Coordinate zb = queue.poll(); for(int direction = 0; direction < 4; direction++) { Coordinate adj = new Coordinate( zb.x + deltaX[direction], zb.y + deltaY[direction] ); if(!isPeople(adj, grid)) { continue; } people--; grid[adj.x][adj.y] = ZOMBIE; if(people == 0) { return days; } queue.offer(adj); } } } return -1; } public boolean isPeople(Coordinate adj, int[][] grid) { int row = grid.length; int col = grid[0].length; if(adj.x < 0 || adj.x >= row) { return false; } if(adj.y < 0 || adj.y >= col) { return false; } return grid[adj.x][adj.y] == PEOPLE; } }
531. Six Degrees
https://www.lintcode.com/problem/six-degrees/description?_from=ladder&&fromId=1
/** * Definition for Undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { * label = x; * neighbors = new ArrayList<UndirectedGraphNode>(); * } * }; */ public class Solution { /* * @param graph: a list of Undirected graph node * @param s: Undirected graph node * @param t: Undirected graph nodes * @return: an integer */ public int sixDegrees(List<UndirectedGraphNode> graph, UndirectedGraphNode s, UndirectedGraphNode t) { // write your code here if(graph == null) { return 0; } if(s == t) { return 0; } Set<UndirectedGraphNode> set = new HashSet<>(); Queue<UndirectedGraphNode> queue = new LinkedList<>(); queue.offer(s); set.add(s); int steps = 0; while(!queue.isEmpty()) { int size = queue.size(); steps++; for(int i = 0; i < size; i++) { UndirectedGraphNode node = queue.poll(); for(UndirectedGraphNode neighbor: node.neighbors) { if(t == neighbor) { return steps; } if(!set.contains(neighbor)) { set.add(neighbor); queue.offer(neighbor); } } } } return -1; } }
178. Graph Valid Tree
https://www.lintcode.com/problem/graph-valid-tree/description?_from=ladder&&fromId=1
public class Solution { /** * @param n: An integer * @param edges: a list of undirected edges * @return: true if it's a valid tree, or false */ public boolean validTree(int n, int[][] edges) { // write your code here if(n <= 0) { return false; } if(edges.length != n - 1) { return false; } Map<Integer, Set<Integer>> map = initializeGraph(n, edges); Queue<Integer> queue = new LinkedList<>(); Set<Integer> set = new HashSet<>(); queue.offer(edges[0][0]); set.add(edges[0][0]); while(!queue.isEmpty()) { Integer node = queue.poll(); for(Integer neighbor: map.get(node)) { if(set.contains(neighbor)) { continue; } queue.offer(neighbor); set.add(neighbor); } } // System.out.println(set.size()); return set.size() == n; } public Map<Integer, Set<Integer>> initializeGraph(int n, int[][] edges) { Map<Integer, Set<Integer>> map = new HashMap<>(); for(int i = 0; i < n; i++) { map.put(i, new HashSet<>()); } for(int i = 0; i < edges.length; i++) { int start = edges[i][0]; int end = edges[i][1]; map.get(start).add(end); //map.get(end).add(start); } return map; } }
431. Connected Component in Undirected Graph
/** * Definition for Undirected graph. * class UndirectedGraphNode { * int label; * ArrayList<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */ public class Solution { /* * @param nodes: a array of Undirected graph node * @return: a connected set of a Undirected graph */ public List<List<Integer>> connectedSet(List<UndirectedGraphNode> nodes) { // write your code here List<List<Integer>> result = new ArrayList<>(); if(nodes == null) { return result; } Map<UndirectedGraphNode, Boolean> map = new HashMap<>(); for(UndirectedGraphNode node: nodes) { map.put(node, false); } for(UndirectedGraphNode node: nodes) { if(map.get(node) == false) { bfs(node, result, map); } } return result; } public void bfs(UndirectedGraphNode node, List<List<Integer>> result, Map<UndirectedGraphNode, Boolean> map) { List<Integer> curr = new LinkedList<>(); Set<UndirectedGraphNode> set = new HashSet<>(); Queue<UndirectedGraphNode> queue = new LinkedList<>(); map.put(node, true); queue.offer(node); set.add(node); while(!queue.isEmpty()) { UndirectedGraphNode n = queue.poll(); curr.add(n.label); for(UndirectedGraphNode neighbor : n.neighbors) { if(!set.contains(neighbor)) { map.put(neighbor, true); queue.offer(neighbor); set.add(neighbor); } } } Collections.sort(curr); result.add(curr); } }
71 Binary Tree Level Order Traversal
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: A Tree * @return: A list of lists of integer include the zigzag level order traversal of its nodes' values. */ public List<List<Integer>> zigzagLevelOrder(TreeNode root) { // write your code here List<List<Integer>> result = new LinkedList<>(); if(root == null) { return result; } Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); boolean leftToRight = true; while(!queue.isEmpty()) { List<Integer> curr = new LinkedList<>(); int size = queue.size(); for(int i = 0; i < size; i++) { TreeNode node = queue.poll(); if(leftToRight) { curr.add(node.val); } else { curr.add(0, node.val); } if(node.left != null) { queue.offer(node.left); } if(node.right != null) { queue.offer(node.right); } } result.add(curr); leftToRight = !leftToRight; } return result; } }
70. Binary Tree Level Order Traversal
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: A tree * @return: buttom-up level order a list of lists of integer */ public List<List<Integer>> levelOrderBottom(TreeNode root) { // write your code here List<List<Integer>> result = new LinkedList<>(); if(root == null) { return result; } Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while(!queue.isEmpty()) { List<Integer> curr = new LinkedList<>(); int size = queue.size(); for(int i = 0; i < size; i++) { TreeNode node = queue.poll(); curr.add(node.val); if(node.left != null) { queue.offer(node.left); } if(node.right != null) { queue.offer(node.right); } } result.add(0, curr); } return result; } }