#include <stdio.h>
#include <string.h>
typedef struct k
{
char data;
struct k *left,*right;
}node;
node *Scan()
{
node *t;
char ch;
if((ch=getchar())=='#')return NULL;
t=(node *)malloc(sizeof(node));
t->data=ch;
t->left=Scan();
t->right=Scan();
return t;
}
void Print(node *head)
{
if(head==NULL)return;
printf("%c",head->data);
Print(head->left);
Print(head->right);
}
/*中序:void Print(node *head)
{
if(head==NULL)return;
Print(head->left);
printf("%c",head->data);
Print(head->right);
}*/
/*后序:void Print(node *head)
{
if(head==NULL)return;
Print(head->left);
Print(head->right);
printf("%c",head->data);
}*/
int main()
{
node * head=Scan();
Print(head);
return 0;
}
二叉树前序遍历读入和输出模板-递归算法
最新推荐文章于 2022-10-10 11:07:31 发布