二叉树的节点个数等于左子树的节点数加上右子树的节点数再加上根节点数1。
递归算法:
template<class ElemType>
int BinaryTree<ElemType>::NodeCountHelp(const BinTreeNode<ElemType>* r)const
{
if (r == NULL) return 0;
else return NodeCountHelp(r->leftChild) + NodeCountHelp(r->rightChild) + 1;
}
二叉树高的定义:空树高为0,非空树高为其左右子树高的最大值加1
算法代码:
template<class ElemType>
int BinaryTree<ElemType>::HeightHelp(const BinTreeNode<ElemType>* r)const
{
if (r == NULL)
{
return 0;
}
else
{
int lHeight = HeightHelp(r->leftChild);
int rHeight = HeightHelp(r->rightChild);
return (lHeight> rHeight? lHeight: rHeight)+1;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)