# 求二叉树度为0，1，2的结点个数

3 篇文章 0 订阅
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 50

typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

BiTree CreateBiTree( ){
char ch;
scanf("%c",&ch);
if(ch=='#') return NULL;
else{
BiTree T = (BiTree)malloc(sizeof(BiTNode));
T -> lchild  = T -> rchild = NULL;
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
return T;
}
}
//统计二叉树中度为0的结点个数
int Degree_0(BiTree T){
if(T==NULL)
return 0;
if(T->lchild==NULL&&T->rchild==NULL)
return Degree_0(T->lchild)+Degree_0(T->rchild)+1;
else
return Degree_0(T->lchild)+Degree_0(T->rchild);
}
//统计二叉树中度为1的结点个数
int Degree_1(BiTree T){
if(T==NULL)
return 0;
if((T->lchild==NULL&&T->rchild!=NULL)||(T->lchild!=NULL&&T->rchild==NULL))
return Degree_1(T->lchild)+Degree_1(T->rchild)+1;
else
return Degree_1(T->lchild)+Degree_1(T->rchild);
}
//统计二叉树中度为2的结点个数
int Degree_2(BiTree T){
if(T==NULL)
return 0;
if((T->lchild!=NULL&&T->rchild!=NULL))
return Degree_2(T->lchild)+Degree_2(T->rchild)+1;
else
return Degree_2(T->lchild)+Degree_2(T->rchild);
}


• 12
点赞
• 0
评论
• 28
收藏
• 一键三连
• 扫一扫，分享海报

01-10
11-19 9356

04-08 1万+
11-02 6323
06-19 3939
07-20 1万+
05-01 2344
07-24 2487
04-28 1949
12-02