My Java DFS Solution with thought process
\n/* \nMy thought process\nSo here we can first actually build the graph and I uses a List<List<integer>> for it.\nHere if we are having parent->child, then we 
```\n/* \nMy thought process\nSo here we can first actually build the graph and I uses a List<List<integer>> for it.\nHere if we are having parent->child, then we take the sign as +ve and if vice versa we consider it as negative.\nFor traditional dfs, we need to keep track of the visited node thus we declare a boolean array for it.\nNow do the dfs from node 0.\n\nSo the dfs function actually looks for the parent->child relation node and this has to be reversed. So count of parent->child relation is tracked here and the count is returned.\n\nSo we looks for the neighbours of the current node and traverse throught it. Then we check whether we already visited it, if no then update the count + do the dfs with the current child + add 1 if the child > 0, ie from parent -> child  or 0 ie then it  will be child -> parent.\n\nFinally return the count and thats the problem.\n*/\n\n\n// 1. Convert the given into graph\n// 2. Do the dfs and look for the forward direction.\n// 3. Count of forward direction will give our answer.\n\nclass Solution {\n    public int minReorder(int n, int[][] connections) {\n        List<List<Integer>> adj = new ArrayList<>();\n        for (int i=0; i<n; i++) {\n            adj.add(new LinkedList<>());\n        }\n        for (int [] c:connections) {\n            adj.get(c[0]).add(c[1]);\n            adj.get(c[1]).add(-c[0]);\n        }\n        boolean [] visited = new boolean[n];\n        return dfs(adj, visited, 0);\n    }\n    public int dfs(List<List<Integer>>adj, boolean [] visited, int v) {\n        int reOrderCount = 0;\n        visited[v] = true;\n        List<Integer> children = adj.get(v);\n        for (Integer child: children) {\n            if (!visited[Math.abs(child)])\n                reOrderCount += dfs(adj, visited, Math.abs(child)) + (child > 0 ? 1:0);\n        }\n        return reOrderCount;\n    }\n}\n```
['Depth-First Search', 'Graph', 'Java']