简体   繁体   中英

What am I missing with this failed, C++ string comparison?

I am trying to compare a string with a binary search tree, and the below code works on the very first one, but then fails on every other one--even though I have checked to make sure it is recursively checking the tree. Thanks!

bool BST::compareIt(Node* current, string name)
{
    if (name == current->title)
       return true;
    if (current->left != NULL)
        compareIt(current->left, name);
    if (current->right != NULL)
        compareIt(current->right, name);
    return false;
 }

I

You need to return the result of your recursive calls, eg

if (current->left != NULL)
    return compareIt(current->left, name);

and similarly for the right hand branch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM