def rightMostPath(graph, root, target, path = None):
if path == None:
path = []
path.append(root)
if root == target:
return path
if root not in graph.keys():
return None
for v in graph[root]:
if v not in path:
ep = rightMostPath(graph, v, target, path)
if ep:
return ep
return None
if __name__ == '__main__':
graph = {0: [1], 1: [0, 2, 3], 2: [1], 3: [1]}
R = rightMostPath(graph, 0, 3)
print(R)
上面的代码将返回路径中的答案为[0,1,2,3],当绘制出这个简单的图形后,很明显路径应为[0,1,3]。我想知道是什么原因导致这种情况发生,因为我看到的所有地方都指向这种类型的路径搜索Python中的图形结构。