创建一个由10个节点组成的二叉树结构,并按前根、中根、后根对该二叉树进行遍历,并输出遍历结果(c语言)

用c语言写
2025-04-07 22:32:44
推荐回答(2个)
回答1:

#include
struct Bitree
{
char c;
struct Bitree *l;
struct Bitree *r;
};
struct Bitree * create(){
char ch;
struct Bitree *s;
scanf("%c",&ch);
if(ch=='#')
return NULL;
else
{
s=(struct Bitree *)malloc(sizeof(struct Bitree));
s->c=ch;
s->l=create();
s->r=create();
return s;

}
}
void preorder(struct Bitree *root)
{
if(root!=NULL)
{
printf("%c",root->c);
preorder(root->l);
preorder(root->r);
}
}
void inorder(struct Bitree *root)
{
if(root!=NULL)
{
inorder(root->l);
printf("%c",root->c);
inorder(root->r);
}
}
void lastorder(struct Bitree *root)
{
if(root!=NULL)
{
lastorder(root->l);
lastorder(root->r);
printf("%c",root->c);
}
}

回答2:

遍历不会还是整个都不会?