<pre name="code" class="cpp">#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef struct node
{
char x;
struct node* lc,* rc;
}node,*tree;
char s[1000];
int m;
void build(tree &t)
{
int l=strlen(s);
if(m>=l) return ;
if(s[m]=='#')
{
m++;return;
}
t=(tree)malloc(sizeof (node));
t->x=s[m++];
t->lc=NULL;
t->rc=NULL;
build(t->lc);
build(t->rc);
}
void in(tree t)
{
if(t)
{
in(t->lc);
printf("%c ",t->x);
in(t->rc);
}
}
int main()
{
tree t;
while(~scanf("%s",s))
{
m=0;
build(t);
in(t);
printf("\n");
}
return 0;
}
//abc##de#g##f### 先序建树
//c b e g d f a 中序结果
先序建二叉树,中序遍历
最新推荐文章于 2023-10-26 14:38:45 发布