MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / dfs

Function dfs

javascript/0332-reconstruct-itinerary.js:14–34  ·  view source on GitHub ↗
(tickets, graph, city = 'JFK', path = ['JFK'])

Source from the content-addressed store, hash-verified

12};
13
14const dfs = (tickets, graph, city = 'JFK', path = ['JFK']) => {
15 const isBaseCase = path.length === tickets.length + 1;
16 if (isBaseCase) return true;
17
18 const queue = graph.get(city) || [];
19
20 const isEmpty = queue.length === 0;
21 if (isEmpty) return false;
22
23 for (const nextCity of queue.slice()) {
24 path.push(nextCity);
25 queue.shift();
26
27 if (dfs(tickets, graph, nextCity, path)) return path;
28
29 path.pop();
30 queue.push(nextCity);
31 }
32
33 return false;
34};
35
36const buildGraph = (tickets, graph = new Map()) => {
37 for (const [src, dst] of tickets) {

Callers 1

findItineraryFunction · 0.70

Calls 3

getMethod · 0.45
pushMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected