代码储存...(有空再解释...)
样例
输入:abc##d##efg###h##
输出:
abcdefgh
cbdagfeh
cdbgfhea
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct node{
char data;
struct node *lchild,*rchild;
}Node,*tree;
void createtree(tree *t){//建立
char ch;
cin>>ch;
if(ch=='#')*t=NULL;
else{
*t=(Node *)malloc(sizeof(Node));
(*t)->data=ch;
createtree(&(*t)->lchild);
createtree(&(*t)->rchild);
}
}
void ptree(tree t){//前序
if(t==NULL)return;
cout<<t->data;
ptree(t->lchild);
ptree(t->rchild);
}
void mtree(tree t){//中序
if(t==NULL)return;;
mtree(t->lchild);
cout<<t->data;
mtree(t->rchild);
}
void ltree(tree t){//后序
if(t==NULL)return;
ltree(t->lchild);
ltree(t->rchild);
cout<<t->data;
}
int main(){
tree T;
createtree(&T);
ptree(T);
cout<<endl;
mtree(T);
cout<<endl;
ltree(T);
cout<<endl;
return 0;
}
网友评论