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

说明:叶子即无左儿子和右儿子。可在建树的同时进行判断是否无两个儿子,若没有,计数器自增 

(OS:新人,有错误请指出,一起成长!)

#include <bits/stdc++.h>
struct tree
{
    struct tree *l,*r;
    char data;
};
char s[101];
int i,amt;
tree *creat(tree *t)
{
    if(s[i++]==',')
        t=NULL;
    else
    {
        t=new tree;
        t->data=s[i];
        t->l=creat(t->l);
        t->r=creat(t->r);
        if(t->l==NULL&&t->r==NULL)
            amt++;
    }
    return t;
}
int main()
{
    while(scanf("%s",s)!=EOF)
    {
        i=0;
        amt=0;
        tree *t;
        t=creat(t);
        printf("%d\n",amt);
    }
    return 0;
}
发布了3 篇原创文章 · 获赞 4 · 访问量 689

猜你喜欢

转载自blog.csdn.net/pio_ke/article/details/81636590
今日推荐