#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");
}
先序建树中序后序遍历及求叶子节点个数
最新推荐文章于 2023-09-23 09:38:45 发布
本文介绍了一种使用C++实现二叉树的方法,包括前序、中序和后序遍历,并通过输入字符的方式创建二叉树。文章详细展示了如何递归地构建树结构及遍历算法。
1598

被折叠的 条评论
为什么被折叠?



