2016-12-02 131 views
-2

我必須在二叉樹中實現字符串搜索方法。它將節點*作爲輸入,並再次輸出節點*。二叉樹搜索字符串C++

問題是由於某些指針異常它不能正常工作,它給了我錯誤。

如果我不清楚,請告訴我。

預先感謝您

-var創建:無法創建可變對象錯誤

zoo_tree::node* tree_tools::search(zoo_tree::node* from,string animal) { 
if (from != NULL) { 
    if (from->question == animal) { 
     return from; 
    } 
    if (from->question != animal) { 
     search(from->left, animal); 
     search(from->right, animal); 

    } 
} 
return NULL; 

}

然而,上述工程的代碼,有什麼區別?

zoo_tree ::節點* tree_tools ::搜索(zoo_tree ::從,串動物節點*){

if (from == NULL) 
    return NULL; 

if (from->question == animal) 
    return from; 

if (from->question != animal) 
{ 
    search(from->left, animal); 
    search(from->right, animal); 
} 

}

回答

0

明顯的錯誤在你的代碼:遞歸調用search()不要」當它不爲null時返回實例,因爲遞歸調用必須遞歸返回。這意味着除非根是搜索到的節點,否則將返回NULL。

+0

非常感謝,我已更正了代碼。 –