基于上一篇我发的二叉树模板函数,添加一个子节点个数的函数,
int SonCountHelp(const BinTreeNode<ElemType>* r) const;//子节点的个数
int SonCount() const;
数据结构--二叉树构造模板_m0_53252742的博客-CSDN博客
inline int BinaryTree<ElemType>::SonCount() const
{
return SonCountHelp(root);
}
template<class ElemType>
inline int BinaryTree<ElemType>::SonCountHelp(const BinTreeNode<ElemType>* r) const
{
if (r == NULL)
return 0;
else if (r->leftChild == NULL || r->rightChild == NULL)
return SonCountHelp(r->leftChild) + SonCountHelp(r->rightChild) + 1;
else if (r->leftChild != NULL)
SonCountHelp(r->leftChild);
else if (r->rightChild != NULL)
SonCountHelp(r->rightChild);
}