Skip to main content

763. Partition Labels

https://leetcode.com/problems/all-paths-from-source-to-target/

Python

class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
self.result = []
self.travel(graph, 0, [])
return self.result

def travel(self, graph, node, path):
path.append(node)

if node == len(graph)-1:
# Reach the end
self.result.append(path[:])
path.pop()
return

for next_node in graph[node]:
self.travel(graph, next_node, path)

path.pop()

return path