#include <iostream>
using namespace std;
int n = 0;
int sum = 0;
struct Bnode{
int data;
Bnode *lch;
Bnode *rch;
};
Bnode*creat(){
Bnode *p;
int x;
cin >> x;
if(x == 0) p = NULL;
else{
p = new(Bnode);
p -> data = x;
p -> lch = creat();
p -> rch = creat();
}
return p;
};
void firstF(Bnode *p){
if(p == NULL) return;
else{
cout << p ->data << endl;
firstF(p ->lch);
firstF(p ->rch);
}
}
void midF(Bnode *p){
if(p == NULL) return;
else{
firstF(p ->lch);
cout << p ->data << endl;
firstF(p ->rch);
}
}
void enF(Bnode *p){
if(p == NULL) return;
else{
firstF(p ->lch);
firstF(p ->rch);
cout << p ->data << endl;
}
}
void leaf(Bnode *p){
if(p != NULL){
if(p ->lch == NULL && p ->rch == NULL)
n++;
leaf(p ->lch);
leaf(p ->rch);
}
}
int dep(Bnode *p){
if(p == NULL) return 0;
else{
int d1 = dep(p ->lch);
int d2 = dep(p ->rch);
return 1+(d1>d2?d1:d2);
}
}
void num(Bnode *p){
if(p != NULL){
sum++;
num(p ->lch);
num(p ->rch);
}
}
int main(){
Bnode *h;
h = creat();
printf("这是先序遍历:\n");
firstF(h);
printf("这是中序遍历:\n");
midF(h);
printf("这是后序遍历:\n");
enF(h);
leaf(h);
printf("共有%d片叶子!\n",n);
int dp = dep(h);
printf("树深:%d\n",dp);
num(h);
printf("%d\n",sum);
return 0;
}
/*
1
2
4
0
0
0
3
5
0
7
0
0
6
8
0
0
9
0
0
×/