【发布时间】:2020-01-13 13:26:52
【问题描述】:
我正在学习递归,我有以下树:
# cough
# Yes / \ No
# fever healthy
# Yes / \ No
# influenza cold
使用以下代码:
def diagnose_helper(symptoms, node, symptoms_checked):
if node.positive_child == None and node.negative_child == None:
print(node.data, "In")
return node.data
for symp in symptoms:
if symp == node.data and symp not in symptoms_checked:
symptoms_checked.append(symp)
return diagnose_helper(symptoms, node.positive_child,
symptoms_checked)
else:
return diagnose_helper(symptoms, node.negative_child,
symptoms_checked)
检查症状是否在列表中,并返回当前情况。 即对于这个输入:
diagnose_helper(["cough, fever"], root, [])
它应该返回“流感”,因为咳嗽 -> 是并且发烧 -> 是的,但它实际上返回的是感冒。 我似乎找不到原因,如果有人有任何想法,我会接受。
【问题讨论】: