有关二叉树的递归算法

1 3 6 8 0 0 0 4 0 0 2 7 0 0 0

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<stack>
#include<queue>
#include<cmath>
#include<stack>
#include<list>
#include<map>
#include<set>
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define gi(x) scanf("%d",&x)
#define gi2(x,y) scanf("%d%d",&x,&y)
#define gll(x) scanf("%lld",&x)
#define gll2(x,y) scanf("%lld%lld",&x,&y)
#define gc(x) scanf("%c",&x)
#define gc2(x,y) scanf("%c%c",&x,&y)
using namespace std;
const double eps=1e-8;
typedef long long ll;
const int MAXN=100005;
const int mod=1000000007;
const ll llinf = (ll)(1e18) + 500;
const int inf=0x3f3f3f3f;
int num[3];
int num_of_node=0;
int cnt=0;
int b[100];
int h=0;
int maxn=-MAXN;
template <class T>
class Node {
public:
Node *lson;
Node *rson;
T data;
};
template <class T>
class binarytree{
private:
Node<T>* root;
public:
binarytree(){
root=NULL;
}
void Create(){
create(root);
}
Node<T>* getroot(){
return root;
}
void create(Node<T>* &p){
T c;
cin>>c;
if(c==0){
p=NULL;
return;
}
p=new Node<T>;
p->data=c;
create(p->lson);
create(p->rson);
}
bool isEmpty(){
if(root==NULL)return true;
return false;
}
void bdisplay(){//宽度优先
queue< Node<T>* >q;
q.push(root);
while(!q.empty()){
Node<T>* u=q.front();
q.pop();
cout<<u->data<<endl;
if(u->lson){
q.push(u->lson);
}
if(u->rson){
q.push(u->rson);
}
}
}

void display(){//深度优先
if(isEmpty())printf("NULL\n");
else visit(root),cout<<endl;
}
void visit(Node<T>* p){
if(p==NULL)return;
cout<<p->data<<' ';
visit(p->lson),visit(p->rson);
}

void caldu(){//前6个小题
if(isEmpty()){printf("NULL\n");return;}
//b[1]=1;
calduvisit(root,1);
for(int i=0;i<3;i++){
printf("度为%d的结点的个数为：",i);
cout<<num[i]<<endl;
}
cout<<"树的高度为："<<h<<endl;
for(int i=1;i<=h;i++){
}
cout<<"最大的元素值为："<<maxn<<endl;
}
void calduvisit(Node<T>* p,int s){
if(p==NULL)return;
b[s]++;
maxn=max(maxn,p->data);
h=max(s,h);
if(p->lson==NULL&&p->rson==NULL){
num[0]++;
}else if(p->lson!=NULL&&p->rson!=NULL){
num[2]++;
}else {
num[1]++;
}
calduvisit(p->lson,s+1),calduvisit(p->rson,s+1);
}

void deleteleaf(){
if(isEmpty()){printf("NULL\n");return;}
if(root->lson==NULL&&root->rson==NULL){
cout<<"只有一个根节点"<<endl;
return ;
}
deleleaf(root);
}
void deleleaf(Node<T>* p){
if(p==NULL)return;
if(p->lson!=NULL){
Node<T>* plson=p->lson;
if(plson->lson==NULL&&plson->rson==NULL){
delete p->lson;
p->lson=NULL;
}
}
if(p->rson!=NULL){
Node<T>* prson=p->rson;
if(prson->lson==NULL&&prson->rson==NULL){
delete p->rson;
p->rson=NULL;
}
}
deleleaf(p->lson),deleleaf(p->rson);
}
void Exchange(){
if(root==NULL){printf("NULL\n");return;}
else exchange(root);
}
void exchange(Node<T>* &p){
if(p==NULL)return ;
swap(p->lson, p->rson);
exchange(p->lson),exchange(p->rson);
}

void deletefree(Node<T>* &p){
if(p==NULL)return ;
deletefree(p->lson);
deletefree(p->rson);
//	cout<<p->data<<' ';
delete p;
}
bool is_complete(Node<T>* p){
if(isEmpty()){printf("NULL\n");return false;}
queue<Node<T>* >q;
q.push(p);
//p=q.pop();
while(q.front()!=NULL){
p=q.front();
q.pop();
q.push(p->lson);
q.push(p->rson);
}
while (!q.empty()) {
p=q.front();
if(p!=NULL)return false;
q.pop();
}
return true;
}
~binarytree(){
deletefree(root);
}
};//1 3 6 8 0 0 0 4 0 0 2 7 0 0 0
int main(){
mem(num, 0);
mem(b,0);
binarytree<int>tree;
tree.Create();
if(tree.is_complete(tree.getroot())){
printf("是完全二叉树\n");
}else printf("不是完全二叉树\n");
tree.caldu();

tree.Exchange();
cout<<"交换后的先序为：";
tree.display();

tree.deleteleaf();
cout<<"删除叶子后的先序为：";
tree.display();

return 0;
}

10-22 2805
01-29 185
02-23 126
05-04 1359
03-08 1万+
01-06 4432
10-01 1060
04-09 1819
06-21 85
01-07 4393
03-05 153
02-19 4055
02-27 154
11-08 9万+
11-14 836
05-28 484