Last active
August 11, 2019 07:25
-
-
Save vansika/26fb22c2ed281bcb866861df3e602ecc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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