#include <stdio.h>
#include <stdlib.h>
typedef struct node{
char data;
struct node *llink;
struct node *rlink;
}NODE;
NODE*build()//
{
NODE *tree;
char c;
scanf("%c",&c);
if(c=='#') tree = NULL;
else
{
tree=(NODE*)malloc(sizeof(NODE));
tree->data=c;
tree->llink=build();
tree->rlink=build();
}
return tree;
}
void houxu(NODE*tree)
{
NODE*pt=tree;
int tt[100],top=-1;
NODE*s[100];
do
{
while(pt!=NULL)
{
s[++top]=pt;
tt[top]=0;
pt=pt->llink;
}
if(top>=0)
{
if(!tt[top])
{
pt=s[top];
tt[top]=1;
pt=pt->rlink;
}
else printf("%c",s[top--]->data);
}
}while((pt!=NULL)||(top!=-1));
}
int main()
{
printf("请输入树的结点:\n");
NODE*tree=(NODE*)malloc(sizeof(NODE));
tree=build();
printf("后序遍历的结果是:\n");
houxu(tree);
printf("\n");
return 0;
}
/*测试数据
abd##e##cf###
输出:debfca
*/
#include <stdlib.h>
typedef struct node{
char data;
struct node *llink;
struct node *rlink;
}NODE;
NODE*build()//
{
NODE *tree;
char c;
scanf("%c",&c);
if(c=='#') tree = NULL;
else
{
tree=(NODE*)malloc(sizeof(NODE));
tree->data=c;
tree->llink=build();
tree->rlink=build();
}
return tree;
}
void houxu(NODE*tree)
{
NODE*pt=tree;
int tt[100],top=-1;
NODE*s[100];
do
{
while(pt!=NULL)
{
s[++top]=pt;
tt[top]=0;
pt=pt->llink;
}
if(top>=0)
{
if(!tt[top])
{
pt=s[top];
tt[top]=1;
pt=pt->rlink;
}
else printf("%c",s[top--]->data);
}
}while((pt!=NULL)||(top!=-1));
}
int main()
{
printf("请输入树的结点:\n");
NODE*tree=(NODE*)malloc(sizeof(NODE));
tree=build();
printf("后序遍历的结果是:\n");
houxu(tree);
printf("\n");
return 0;
}
/*测试数据
abd##e##cf###
输出:debfca
*/