Skip to content

Instantly share code, notes, and snippets.

@vansika
Last active August 11, 2019 07:25
Show Gist options
  • Save vansika/26fb22c2ed281bcb866861df3e602ecc to your computer and use it in GitHub Desktop.
Save vansika/26fb22c2ed281bcb866861df3e602ecc to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# All paths from source to destination
# source = 0
# destination = n - 1
Input: [[1,2], [3], [3], []]
Output: [[0,1,3],[0,2,3]]
class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
dest = len(graph) - 1
path = []
curr_path = []
def getPath(node):
curr_path.append(node)
if node == dest:
path.append(curr_path[:])
return
for i in graph[node]:
getPath(i)
curr_path.pop()
getPath(0)
return path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment