This is a code for DFS all paths from source to the destination where source = 0 and destination = size of the graph - 1. In Line 12, why can't I simply do res.append((path))
since the path is already a list?
from collections import defaultdict
class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
adjlist = defaultdict(list)
for i in range(len(graph)):
adjlist[i].extend(graph[i])
start = 0
destination = len(graph) - 1
res = []
def dfs(node,path):
nonlocal res
if node == destination:
res.append(list(path))
return
if node in adjlist:
for nextNode in adjlist[node]:
path.append(nextNode)
dfs(nextNode,path)
path.pop()
dfs(0,[0])
print(res)