c ++搜索树中的节点

时间:2013-05-18 00:03:06

标签: c++ recursion tree target

在我的程序中,我递归地尝试在给定目标的同时在树中找到节点,但我无法使其工作!

    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);

1 个答案:

答案 0 :(得分:3)

问题在于:

if(find_node(tmp->m_left, target))
{
    return tmp;
}

您应该返回find_node的结果:

tmp = find_node(cur->m_left, target);
if (tmp) return tmp;