“ Ctrl AC!一起 AC!”
目录
数据结构定义:
typedef char datatype;
typedef struct node{
datatype data;
struct node *lchild,*rchild;
}bintnode;
typedef bintnode *bintree;
bintree root;
前序遍历二叉树递归算法:
void preorder(bintree t){
if(t){
printf("%c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
中序遍历二叉树递归算法:
void inorder(bintree t){
if(t){
inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}
后序遍历二叉树递归算法:
void postorder(bintree t){
if(t){
postorder(t->lchild);
postorder(t->rchild);
printf("%c",t->data);
}
}
根据前序遍历以及附带信息建立二叉树:
(“#”表示空树)
bintree createbintree(){
char ch;
bintree t;
if((ch=getchar())=='#'){
t=NULL;
}
else{
t=(bintnode*)malloc(sizeof(bintnode));
t->data=ch;
t->lchild=createbintree();
t->rchild=createbintree();
}
return t;
}
比如输入abd#e##fg###c##,会生成:
感谢阅读!!!
“ Ctrl AC!一起 AC!”