I have a function for tree traversal using depth limited search algorithm which is defined below:
Global declared data
visited = []
queue = []
parse_order = []
and the function:
def depth_limited_search(visited, graph, node, limit):
if limit >= 1:
if node not in visited:
parse_order.append(node)
visited.append(node)
for neighbour in graph[node]:
depth_limited_search(visited, graph, neighbour, limit-1)
else:
parse_order.append(node)
print(parse_order)
return parse_order
Graph:
{'A': ['B', 'C', 'D'], 'B': ['E'], 'C': ['F', 'G'], 'D': ['H'], 'E': ['I'], 'F': ['J'], 'G': [], 'H': [], 'I': [], 'J': []}
And for use of the function like below:
x = depth_limited_search(visited, graph, 'A', 1)
print("
x=", x)
The output is:
['A', 'B']
['A', 'B', 'C']
['A', 'B', 'C', 'D']
x= None
Last list printed is the correct answer, how I can obtain that when assigning function return value to a variable?
question from:
https://stackoverflow.com/questions/65649281/python-function-return-none-when-it-should-return-a-list 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…