I have a basic function that does an in order traversal in C++:
void inorder(Node *root)
{
if(root != NULL)
{
inorder(root->left);
cout<<root->data<<endl;
inorder(root->right);
}
}
However, I would like to return a list as a result of this in order traversal. But the key thing is how can we determine when this recursive function actually ends and I can return the list. Here is the code that I've done so far;
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?
}
I think the answer of this question would also help me with the core concept of recursion as well