Java || BFS || O(n)
\n    public int minReorder(int n, int[][] connections) {\n        int count = 0;\n\t\tList[] graph = new ArrayList[n];\n\t\tfor (int i = 0; i < n; i++)\n\t\t\t
\n    public int minReorder(int n, int[][] connections) {\n        int count = 0;\n\t\tList<Integer>[] graph = new ArrayList[n];\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tgraph[i] = new ArrayList<Integer>();\n\n\t\tfor (int[] connection : connections) {\n\t\t\tgraph[connection[0]].add(connection[1]);\n\t\t\tgraph[connection[1]].add(-connection[0]);\n\t\t}\n\n\t\tQueue<Integer> queue = new LinkedList<Integer>();\n\t\tboolean[] visited = new boolean[n];\n\t\tvisited[0] = true;\n\t\tqueue.add(0);\n\n\t\twhile (!queue.isEmpty()) {\n\t\t\tint temp = queue.poll();\n\t\t\tList<Integer> nbrs = graph[temp];\n\t\t\tfor (int nbr : nbrs) {\n\t\t\t\tif (visited[Math.abs(nbr)])\n\t\t\t\t\tcontinue;\n                visited[Math.abs(nbr)] = true;\n\t\t\t\tif (nbr > 0)\n\t\t\t\t\tcount++;\n\t\t\t\tqueue.add(Math.abs(nbr));\n\t\t\t}\n\t\t}\n\n\t\treturn count;\n    }\n
[]