#include <stdio.h>
#include <malloc.h>
typedef struct Node
{
char data;
struct Node *lchild;
struct Node *rchild;
}*btree;
btree create_tree()
{
char element;
printf("input element:");
scanf(" %c",&element);
if(element=='#')
{
return NULL;
}
btree T=(btree)malloc(sizeof(struct Node));
T->data=element;
printf("%c left:\n",element);
T->lchild=create_tree();
printf("%c right:\n",element);
T->rchild=create_tree();
return T;
}
void first_output(btree T)
{
if(T==NULL)
{
return;
}
printf("%c",T->data);
first_output(T->lchild);
first_output(T->rchild);
}
int main() {
btree T=create_tree();
first_output(T);
return 0;
}
二叉树的创建和先序遍历
于 2023-10-01 20:31:02 首次发布