I want to have a vector that pushes in 'R' everytime the tree traverses to the right subtree and pushes in 'L' everytime the tree traverse to a left subtree.
void reverse_preorder(BTree<string>* root)
{ vector<string>a;
if (root != NULL) {
a.push_back("R");
if(root->right == NULL) a.push_back("L");
reverse_preorder(root->right);
reverse_preorder(root->left);
}
}
but for some reason the output im getting is not in the correct order. What am I doing wrong?
Make the vector a member of your class. Then
void reverse_preorder(BTree<string>* root)
{
if (root != NULL) {
if (root->right != NULL) a.push_back("R");
reverse_preorder(root->right);
if (root->left != NULL) a.push_back("L");
reverse_preorder(root->left);
}
}
Though, I don't see how it will help you to do a 'pretty' formatting.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.