数据结构中,经常遇到这样的考题,就是计算二叉树中叶子节点的个数。我用递归是思想来进行计算,具体代码如下:
int GetCountOfLeaves(Bitree *bt){
if (!bt) return 0;//当前结点是空结点
if (!bt->lchlid&& !bt->rchild)
return 1; //当前结点是叶结点
retrun GetCountOfLeaves(bt->lchild) //当前结点是分支结点,结果为左分支的叶结点数
+ GetCountOfLeaves(bt->rchild); //与右分支的叶结点之和
}
判断链表是否升序或降序:
int judgedesc(node *head)
{
if (head == NULL && head -> next == NULL)
{
printf("这是一个空链表");
return 0;
}
else
{
node *p=head->next;
node *q=p->next;
while (q!=NULL)
{
//降序用这句: if (p->data < q->data ) return 0;
//下面是判断升序
if (p->data > q->data ) return 0;//只要有一个不满足就跳出
p=p->next;
q=q->next;
}
return 1;//是升序 就返回1
}
}
{
if (head == NULL && head -> next == NULL)
{
printf("这是一个空链表");
return 0;
}
else
{
node *p=head->next;
node *q=p->next;
while (q!=NULL)
{
//降序用这句: if (p->data < q->data ) return 0;
//下面是判断升序
if (p->data > q->data ) return 0;//只要有一个不满足就跳出
p=p->next;
q=q->next;
}
return 1;//是升序 就返回1
}
}