1、计算二叉树的叶子结点的个数
template<typename DataType>
int BiTree<DataType>::Count(BiNode *bt){
int count;
if(bt == NULL)
count = 0;
else if(bt->lchild==NULL&&bt->rchild==NULL)
count = 1;
else
count = Count(bt->lchild)+Count(bt->rchild);//递归计算叶子解点的个数
return count;
}
2、计算二叉树的最大深度和最小深度
#include <algorithm> //要用max和min函数要先导入这个头文件
template<typename DataType>
int BiTree<DataType>::maxDepth(BiNode *bt){
if (!bt) return 0;
return max(maxDepth(bt->lchild), maxDepth(bt->rchild)) + 1;
}
template<typename DataType>
int BiTree<DataType>::minDepth(BiNode *bt){
// 求最小深度 , 将代码补充在这里
if(!bt) return 0;
return min(minDepth(bt->lchild),minDepth(bt->rchild))+1;
}
3、计算二叉树的结点为2的个数
template<typename DataType>
int NodeCount (BiTree T){
if (T==NULL){
return 0;
}
else if((T->lchild!=NULL)&&(T->rchild!=NULL)){
return 1+NodeCount(T->lchild)+NodeCount(T->rchild);
}
}