Image Explanation🏆- [Complete Intuition - DFS] - C++/Java/Python
Video Solution (Aryan Mittal)\nReorder Routes to Make All Paths Lead to the City Zero by Aryan Mittal\n\n\n\n\n# Approach & Intution\n\n\n\n\n\n\n\n\n\n# Code\n
# Video Solution (`Aryan Mittal`)\n`Reorder Routes to Make All Paths Lead to the City Zero` by `Aryan Mittal`\n![meta4.png](https://assets.leetcode.com/users/images/30b858e7-6f23-4721-bce4-c3df73a6037a_1679635121.8728836.png)\n\n\n\n# Approach & Intution\n![image.png](https://assets.leetcode.com/users/images/6f7886fc-f26a-439f-9673-4f59bbe5d23c_1679623726.6315536.png)\n![image.png](https://assets.leetcode.com/users/images/b927a9ef-71ce-43ac-831c-ac15e810bc6e_1679623741.0327196.png)\n![image.png](https://assets.leetcode.com/users/images/909eab13-0e0a-4be5-83af-4e806186aad7_1679623753.6596923.png)\n![image.png](https://assets.leetcode.com/users/images/ceeef809-6223-47a4-9ded-70493f8986b3_1679623763.8884892.png)\n![image.png](https://assets.leetcode.com/users/images/fe1b3c59-fe4e-4f30-9f6f-e88f0d9c43a0_1679623771.954505.png)\n![image.png](https://assets.leetcode.com/users/images/b56ca480-28f9-4aad-81f3-1e790daab699_1679623801.541086.png)\n![image.png](https://assets.leetcode.com/users/images/7e4a6b8d-ff6c-4c92-a8b1-464a3cefd23f_1679623810.5881977.png)\n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int dfs(vector<vector<int>> &gr, vector<bool> &visited, int from) {\n    auto change = 0;\n    visited[from] = true;\n    for (auto to : gr[from])\n        if (!visited[abs(to)])\n            change += dfs(gr, visited, abs(to)) + (to > 0);\n    return change;        \n    }\n    int minReorder(int n, vector<vector<int>>& connections) {\n        vector<vector<int>> gr(n);\n        for (auto &c : connections) {\n            gr[c[0]].push_back(c[1]);\n            gr[c[1]].push_back(-c[0]);\n        }\n        return dfs(gr, vector<bool>(n) = {}, 0);\n    }\n};\n```\n```Java []\nclass Solution {\n    int dfs(List<List<Integer>> gr, boolean[] visited, int from) {\n        int change = 0;\n        visited[from] = true;\n        for (var to : gr.get(from))\n            if (!visited[Math.abs(to)])\n                change += dfs(gr, visited, Math.abs(to)) + (to > 0 ? 1 : 0);\n        return change;   \n    }\n    public int minReorder(int n, int[][] connections) {\n        List<List<Integer>> gr = new ArrayList<>();\n        for(int i = 0; i < n; ++i) \n            gr.add(new ArrayList<>());\n        for (var c : connections) {\n            gr.get(c[0]).add(c[1]);\n            gr.get(c[1]).add(-c[0]);\n        }\n        return dfs(gr, new boolean[n], 0);\n    }\n}\n```\n```Python []\nclass Solution:\n    def dfs(self, gr, visited, from_node):\n        change = 0\n        visited[from_node] = True\n        for to_node in gr[from_node]:\n            if not visited[abs(to_node)]:\n                change += self.dfs(gr, visited, abs(to_node)) + (1 if to_node > 0 else 0)\n        return change\n\n    def minReorder(self, n, connections):\n        gr = [[] for _ in range(n)]\n        for c in connections:\n            gr[c[0]].append(c[1])\n            gr[c[1]].append(-c[0])\n        visited = [False] * n\n        return self.dfs(gr, visited, 0)\n```\n
['Depth-First Search', 'Graph', 'Python', 'C++', 'Java']