解题思路:
注意事项:
参考代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct tree{
char data;
struct tree *Lchlid,*Rchlid;
}tree;
tree *root;
tree* build_tree(tree *root){
char ch;
scanf("%c",&ch);
if (ch ==' ') {
root=NULL;
}
else{
root=(tree *)malloc(sizeof(tree));
root->data=ch;
root->Lchlid=build_tree(root->Lchlid);
root->Rchlid=build_tree(root->Rchlid);
}
return root;
}
void preprint(tree *root){
if(root==NULL)
return;
else{
printf("%c ",root->data);
preprint(root->Lchlid);
preprint(root->Rchlid);
}
}
void inprint(tree *root){
if(root==NULL) return;
else{
inprint(root->Lchlid);
printf("%c ",root->data);
inprint(root->Rchlid);
}
}
void postprint(tree *root){
if(root==NULL)
return;
else{
postprint(root->Lchlid);
postprint(root->Rchlid);
printf("%c ",root->data);
}
}
int main (){
tree *root;
root=build_tree(root);
preprint(root);
printf("\n");
inprint(root);
printf("\n");
postprint(root);
return 0;
}
0.0分
0 人评分