public static ArrayList<String> serializeTree(TreeNode tree) { ArrayList<String> res = new ArrayList<>(); LinkedList<TreeNode> queue = new LinkedList<>(); queue.addLast(tree); while (!queue.isEmpty()) { TreeNode node = queue.removeFirst(); if (node == null) { res.add(NULL); } else { res.add(String.valueOf(node.val)); queue.add(node.left); queue.add(node.right); } } while (res.size() > 1 && res.get(res.size() - 1).equals(NULL)) { res.remove(res.size() - 1); } return res; }
public static TreeNode createTree(ArrayList<String> list) { LinkedList<NodeWithBranch> queue = new LinkedList<>(); TreeNode head = new TreeNode(0); queue.addLast(new NodeWithBranch(head, 0)); for (String s : list) { NodeWithBranch node = queue.removeFirst(); if (!s.equals(NULL)) { if (node.branch == 0) { node.tree.left = new TreeNode(Integer.parseInt(s)); queue.addLast(new NodeWithBranch(node.tree.left, 0)); queue.addLast(new NodeWithBranch(node.tree.left, 1)); } else { node.tree.right = new TreeNode(Integer.parseInt(s)); queue.addLast(new NodeWithBranch(node.tree.right, 0)); queue.addLast(new NodeWithBranch(node.tree.right, 1)); } } } return head.left; }
public void solve() { int m = in.nextInt(); int n = in.nextInt(); int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = in.nextInt(); } } Set<Point> visited = new HashSet<Point>(); List<Set<Point>> rooms = new ArrayList<Set<Point>>(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { Point cur = new Point(i, j); if (!visited.contains(cur)) { Set<Point> connected = new HashSet<Point>(); LinkedList<Point> list = new LinkedList<Point>(); list.addLast(cur); while (!list.isEmpty()) { Point head = list.removeFirst(); debug(head); if (!visited.contains(head)) { visited.add(head); connected.add(head); if ((a[head.x][head.y] & 1) == 0) { list.addLast(new Point(head.x, head.y - 1)); } if ((a[head.x][head.y] & 1 << 1) == 0) { list.addLast(new Point(head.x - 1, head.y)); } if ((a[head.x][head.y] & 1 << 2) == 0) { list.addLast(new Point(head.x, head.y + 1)); } if ((a[head.x][head.y] & 1 << 3) == 0) { list.addLast(new Point(head.x + 1, head.y)); } debug(visited); } } rooms.add(connected); } } } out.println(rooms.size()); int[][] count = new int[n][m]; int maxRooms = 0; Map<Point, Integer> map = new HashMap<Point, Integer>(); int hashCount = 0; for (Set<Point> room : rooms) { maxRooms = max(maxRooms, room.size()); for (Point x : room) { count[x.x][x.y] = room.size(); map.put(x, hashCount); } hashCount++; } out.println(maxRooms); int maxConnected = 0; int resX = 0, resY = 0; char resD = ' '; for (int j = 0; j < m; j++) { for (int i = n - 1; i >= 0; i--) { if (i > 0 && ((a[i][j] & 1 << 1) != 0) && !map.get(new Point(i, j)).equals(map.get(new Point(i - 1, j))) && count[i][j] + count[i - 1][j] > maxConnected) { maxConnected = count[i][j] + count[i - 1][j]; resX = i; resY = j; resD = 'N'; } if (j < m - 1 && ((a[i][j] & 1 << 2) != 0) && !map.get(new Point(i, j)).equals(map.get(new Point(i, j + 1))) && count[i][j] + count[i][j + 1] > maxConnected) { maxConnected = count[i][j] + count[i][j + 1]; resX = i; resY = j; resD = 'E'; } } } out.println(maxConnected); out.printf("%d %d %c\n", resX + 1, resY + 1, resD); debug(count); }