Reputation: 31
This function prints the preorder traversal of expression tree
void PreOrder(Node root) {
if (root)
{
cout<< root->data<<" ";
PreOrder(root->left);
PreOrder(root->right);
}
}
But want to return pre order traversal of expression tree as a string. but the following code compiles successfully but it does not give any output.
string PreOrder(Node root) {
string s="";
if (root)
{
s=s+root->data;
PreOrder(root->left);
PreOrder(root->right);
}
return s;
}
how should I solve it.
Upvotes: 0
Views: 321
Reputation: 60228
You need to also use the result of the recursive calls to PreOrder
:
s += PreOrder(root->left);
s += PreOrder(root->right);
Upvotes: 2