首页 > 试题广场 >

编写递归算法,计算二叉树中叶子结点的数目。

[问答题]
编写递归算法,计算二叉树中叶子结点的数目。
法一:核心部分为:

DLR(liuyu *root)
/*中序遍历 递归函数*/
{ if (root!=NULL)
{ if ((root->lchild==NULL)&&(root->rchild==NULL)){sum++; printf ("%d\n",root->data);}
DLR (root->lchild);
DLR (root->rchild); }
Return (0);
}

法二:

int LeafCount_BiTree(Bitree T)//求二叉树中叶子结点的数目
{
If (!T) return 0; //空树没有叶子
else if (!T->lchild&&!T->rchild) return 1; //叶子结点
else return Leaf_Count(T->lchild)+Leaf_Count(T->rchild);//左子树的叶子数加
上右子树的叶子数
}//LeafCount_BiTree

发表于 2017-05-13 02:23:02 回复(0)