在我的程序中,我递归地尝试在给定目标的同时在树中找到节点,但我无法使其工作!
Stree::Node * Stree::find_node(Node* cur, string target)
{
Node *tmp = cur;;
if(cur == NULL || tmp == NULL)
return NULL;
if(cur->m_city == target || tmp->m_city == target)
return cur;
if(find_node(tmp->m_left, target))
{
return tmp;
}
else return find_node(cur->m_right, target);
答案 0 :(得分:3)
问题在于:
if(find_node(tmp->m_left, target))
{
return tmp;
}
您应该返回find_node
的结果:
tmp = find_node(cur->m_left, target);
if (tmp) return tmp;