A site's pages link to each other. Write depthFirstSearch(links, start), which crawls the site depth-first from start and returns the pages in the order they're first visited.
links is an adjacency list: links[page] is an array of the pages it links to, in the order they appear on the page. Links are one-way.
Depth-first means: visit a page, then fully explore its first unvisited link before moving on to its second link, and so on. Each page is visited at most once.
const links = {
home: ['blog', 'about'],
blog: ['post-1', 'post-2'],
'post-1': ['home'],
'post-2': [],
about: ['team'],
team: [],
};
depthFirstSearch(links, 'home');
// ['home', 'blog', 'post-1', 'post-2', 'about', 'team']
depthFirstSearch(links, 'about'); // ['about', 'team']
links has no outgoing links.start is not a key in links and isn't linked from anywhere, return [start].Spotted a mistake or a rights concern? Report an issue