Tree traversal and return vector list.
//In case your pre, in, post-order traversal function wants you to return the list, not to print them.
create another function lst1, and pass vector with refernce.
void lst1(struct Node*root, vector<int> &v)
{
if (root==0)
return;
else
v.push_back(root->data);
lst1(root->left,v);
lst1(root->right,v);
}
vector <int> preorder(Node* root)
{
vector<int>v;
lst1(root,v);
return v;
}
{
if (root==0)
return;
else
v.push_back(root->data);
lst1(root->left,v);
lst1(root->right,v);
}
vector <int> preorder(Node* root)
{
vector<int>v;
lst1(root,v);
return v;
}
Comments
Post a Comment