我有做的中序遍歷 C++中的基本功能:遞歸:中序遍歷返回一個列表
void inorder(Node *root)
{
if(root != NULL)
{
inorder(root->left);
cout<<root->data<<endl;
inorder(root->right);
}
}
不過,我想返回一個列表,這個爲了結果遍歷。但關鍵是我們如何確定當這個遞歸函數實際上結束了並且我可以返回列表。這是我迄今爲止所做的代碼;
vector<int> inorder(Node *root, vector<int> listToAdd)
{
if(root != NULL)
{
inorder(root->left, listToAdd);
listToAdd.push_back(root->data);
inorder(root->right, listToAdd);
//return here?
}
// return here?
}
我認爲這個問題的答案也將幫助我遞歸的核心理念以及