#include<iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
typedef struct node
{ char date;
struct node *Tr,*Tl;
}Ttree,*Titree;
int s;
void Creat(Titree *T)
{ char a;
cin>>a;
if(a=='-') *T=NULL;
else {
(*T)=(Titree)malloc(sizeof(Ttree));
(*T)->date=a;
Creat(&(*T)->Tl);
Creat(&(*T)->Tr);
}
}
void zhongxu(Titree T)
{ if(T)
{ zhongxu(T->Tl);
cout<<T->date<<" ";
zhongxu(T->Tr);
}
}
void houxu(Titree T)
{ if(T)
{ houxu(T->Tl);
houxu(T->Tr);
cout<<T->date<<" ";
}
}
void qianxu(Titree T)
{ if(T)
{ cout<<T->date<<" ";
if(!(T->Tl)&&!(T->Tr)) s++;
qianxu(T->Tl);
qianxu(T->Tr);
}
}
int main()
{ Titree T;
s=0;
Creat(&T);
zhongxu(T);
cout<<endl;
houxu(T);
cout<<endl;
qianxu(T);
cout<<endl;
cout<<s<<endl;
system("pause");
}
先序建树中序后序遍历及求叶子节点个数
最新推荐文章于 2022-09-03 14:41:53 发布