int get_leaf_number2(btree *t) //非递归求叶子节点数
{
if(t==NULL)
{
return 0;
}
TOP *first;
first = malloc(sizeof(TOP)); //先创建栈
first->Top = NULL;
btree *p,*q;
p=t;
int number=0;
while(p || (first->Top!=NULL)) //用中序遍历来计算叶子节点数
{
if(p)
{
Stackpush(first,p);
if(p->lchild==NULL && p->rchild==NULL)
number++; //只需要在这进行叶子节点数相加即可
p=p->lchild;
}
else
{
q=Stackpop(first);
p=q->rchild;
}
}
return number;
}
非递归求叶子节点数
于 2021-04-23 16:27:05 首次发布