我正在为我的AI考试而学习,我被困在了Breath first search algorithm
中。根据Breath first search algorithm
,假设robot
想要在下图中从B
导航到P
。
我在使用BFS and DFS
查找遍历时遇到了困难。
考虑一下这件事可能会很受欢迎。
我画了这张图
发布于 2020-08-15 16:11:03
我不太确定实际的问题是什么,但这是我写的一些代码,用于导航二叉树的广度优先:
# Print tree breadth-first
def breadthFirst(node):
stack = [[node]]
while len(stack[-1]) != 0:
newLevel = []
for node in stack[-1]:
if node.left != None:
newLevel.append(node.left)
if node.right != None:
newLevel.append(node.right)
if len(newLevel) == 0:
print('\n'.join('\n'.join(n.val for n in level) for level in stack))
stack.append(newLevel)
https://stackoverflow.com/questions/54899327
复制相似问题