All Paths From Source to Target LeetCode solution in JavaScript
1/**2* @param {number[][]} graph3* @return {number[][]}4*/5const allPathsSourceTarget = function (graph) {6const n = graph.length;7const result = [];8const dfs = (node, path) => {9if (node === n - 1) {10result.push([...path, node]); // Add the current path to the result if we have reached the target node11return;12}13for (const neighbor of graph[node]) {14dfs(neighbor, [...path, node]); // Recursively explore all neighbors of the current node15}16};17dfs(0, []); // Start the DFS from the source node18return result;19};