Backend Engineer
Senior
programming
Given a directed acyclic graph represented as an adjacency list and a starting node, return a list of all nodes reachable from the starting node following the edges of the graph.
Function Signature:
def reachable_nodes(graph: List[List[int]], start: int) -> List[int]: Constraints: - The number of nodes will be between 1 and 10^4.
- Each node is labeled with integers from 0 to n-1, where n is the number of nodes.
Example 1:
Input:
graph = [[1], [2], []], start = 0 Output:
[0, 1, 2] Example 2: Input:
graph = [[2], [2], []], start = 1 Output:
[1, 2]
Suggested Answer