1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#include <iomanip>
#include <algorithm>
struct node
{
int value = 0 ;
node* left = nullptr ;
node* right = nullptr ;
};
std::ostream& print_inorder( const node* root, std::ostream& stm = std::cout )
{
if(root)
{
print_inorder( root->left, stm ) ;
stm << std::setw(5) << root->value ;
print_inorder( root->right, stm ) ;
}
return stm ;
}
node* reverse( node* root )
{
if(root)
{
reverse(root->left) ;
reverse(root->right) ;
using std::swap ;
swap( root->left, root->right ) ;
}
return root ;
}
int main()
{
node n1{1}, n2{2}, n4{4}, n6{6}, n5{ 5, &n1, &n2 }, n7{ 7, &n4, &n6 }, n8{ 8, &n5, &n7 } ;
node* root = &n8 ;
print_inorder(root) << '\n' ;
print_inorder( reverse(root) ) << '\n' ;
}
|