https://leetcode.com/problems/all-paths-from-source-to-target/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution: def allPathsSourceTarget(self, G: List[List[int]]) -> List[List[int]]: def f(start,curr): if start==N-1: Z.append(curr[:]) return for node in G[start]: curr.append(node) f(node,curr) curr.pop()
if not G: return [] N=len(G) Z=[] f(0,[0]) return Z
|