BFS | 94 % Beats | Java - Simple Explanation
\n\n# Approach\n Describe your approach to solving the problem. \nFirst thing is making the graph bidirectional.\nIf an edge is given from a -> b, then we shoul
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst thing is making the graph bidirectional.\nIf an edge is given from a -> b, then we should build the graph\nby edges a -> b and b -> -a .\nHere minus sign indicates that we have created this new edge and it\'s not the given directed edge.\n\n\n\n# Code\n```java []\nclass Solution {\n    int dfs(List<List<Integer>> adj, boolean[] visited, int src) {\n        int change = 0;\n        visited[src] = true;\n        for(int node : adj.get(src)) {\n            if(!visited[Math.abs(node)]) {\n                change += (node > 0 ? 1 : 0);\n                change += dfs(adj, visited, Math.abs(node));\n            }\n        }\n        return change;\n    }\n    public int minReorder(int n, int[][] connections) {\n        List<List<Integer>> adj = new ArrayList<>();\n        for(int i = 0; i < n; i++) adj.add(new ArrayList<>());\n\n        for(int conn[] : connections) {\n            int src = conn[0];\n            int dest = conn[1];\n            adj.get(src).add(dest);\n            adj.get(dest).add(-src);\n        }\n\n        return dfs(adj, new boolean[n], 0);\n    }\n}\n```
['Java']