Depth First Search
Example in TypeScript
function depthFirstSearch(node: Node, target: any): boolean {
if (node.data === target) {
return true;
}
node.visited = true;
for (const neighbor of node.neighbors) {
if (!neighbor.visited) {
if (depthFirstSearch(neighbor, target)) {
return true;
}
}
}
return false;
}Usage
Last updated