JS/TS Solution DFS Approach
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n## Time
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n## Time complexity: O(N)\n- The time complexity of this solution is O(n), where n is the number of cities. This is because we are performing a DFS traversal on the graph, which visits each city only once.\n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n## Space complexity: O(N)\n- The space complexity is O(n), where n is the number of cities. This is because we are storing the adjacency list for each city, as well as the visited array to keep track of visited cities during the DFS traversal.\n\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```javascript []\n/**\n * @param {number} n\n * @param {number[][]} connections\n * @return {number}\n */\nvar minReorder = function (n, connections) {\n    let adjList = {};\n    let reverseRoads = 0;\n    let visited = Array(n).fill(false);\n\n    for (let i = 0; i < n; i++) {\n        adjList[i] = [];\n    }\n\n    // Step 1: Build a bidirectional graph\n    for (let [a, b] of connections) {\n        // Store forward and reverse roads\n        adjList[a].push([b, 1]); // road goes from a to b, so 1 means road is directed away from 0\n        adjList[b].push([a, 0]); // road goes from b to a (reverse connection), 0 means road is directed towards 0\n\n    }\n\n    // Step 2: DFS Traversal\n    let dfs = (node) => {\n        visited[node] = true;\n\n        for (let [neighbor, isReversed] of adjList[node]) {\n            if (!visited[neighbor]) {\n                reverseRoads += isReversed;  // Count if the road is directed away from city 0\n                dfs(neighbor);\n            }\n        }\n    }\n\n    // Step 3: Start DFS from city 0\n    dfs(0);\n\n    return reverseRoads;\n};  \n```
['Depth-First Search', 'Graph', 'TypeScript', 'JavaScript']