#include <string>
#include <memory>
#include <sstream>
#include <iostream>

struct node
{
    using pointer = std::shared_ptr<node> ;
    node( const std::string& d, const pointer& lc, const pointer& rc )
        : data(d), left_child(lc), right_child(rc) {}
    std::string data ;
    pointer left_child ;
    pointer right_child ;
};

std::ostream& write( std::ostream& stm, const node::pointer& tree )
{
    if(tree)
    {
        stm << tree->data << ' ' ;
        write( stm, tree->left_child ) ;
        return write( stm, tree->right_child ) ;
    }
    else return stm << "# " ;
}

node::pointer make_tree( std::istream& stm )
{
    std::string data ;
    if( stm >> data && data != "#" )
    {
        auto left_child = make_tree(stm) ;
        return std::make_shared<node>( data, left_child, make_tree(stm) ) ;
    }
    else return nullptr ;
}

int main()
{
    std::istringstream stm( "root aaa C # # D # # string 1231 # # #" ) ;
    auto tree = make_tree(stm) ;
    write( std::cout, tree ) << '\n' ;
}
