DFS:
def DFS(node,value):
if not node:
return
for ele in node.所有邻居:
if not ele.ischecked: #避免重复检查
ele.ischecked == True #避免重复检查
if ele.value == value:
result.append(ele)
else:
DFS(ele,value)
BFS:
result = []
def BFS(node,value):
nextlist = node.所有邻居
for ele in nextlist:
if not ele.ischecked:
ele.ischecked = True
if ele.value == value:
return ele
else:
把ele的所有邻居append到nextlist中
nextlist.pop() #把当前ele从nextlist中弹出