数据结构实验之二叉树三:统计叶子数

来源:互联网 发布:各大网络负面消息列表 编辑:程序博客网 时间:2024/05/16 05:55




#include<stdio.h>#include<string.h>#include<stdlib.h>char a[100];int i;int count;struct node{    int data;    struct node *lchild;    struct node *rchild;};struct node *creat(){    struct node  *root;    char c;    c=a[i++];    if(c==',')        return NULL;    else    {        root=(struct node *)malloc(sizeof(struct node));        root->data=c;        root->lchild=creat();        root->rchild=creat();    }    return root;}void leaves(struct node  *root){    if(root!=NULL)    {        if(root->lchild==NULL&&root->rchild==NULL)        {            count++;        }        leaves(root->lchild);        leaves(root->rchild);    }}int main(){    struct node *root;    while(~scanf("%s",a))    {        i=0;        count=0;        root=creat();        leaves(root);        printf("%d\n",count);    }    return 0;}


0 0
原创粉丝点击