#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int cnt;
char s[101];
struct node
{
char data;
struct node *rchild;
struct node *lchild;
};
struct node *buildtree()
{
struct node *root;
if(s[cnt++]==',' )root=NULL;
else
{
root=(struct node *)malloc(sizeof(struct node));
root->data=s[cnt-1];
root->rchild=buildtree();
root->lchild=buildtree();
}
return root;
};
int LeafCount (node *root)
{
if(root==NULL)
return 0;
else
{
if(root->lchild==NULL&&root->rchild==NULL)
return 1;
else
return LeafCount(root->lchild)+LeafCount(root->rchild);
}
}
int main()
{
while(cin>>s)
{
struct node *root;
cnt=0;
root=buildtree();
cout<<LeafCount(root)<<endl;
}
}
数据结构实验之二叉树三:统计叶子数
最新推荐文章于 2023-01-29 18:16:23 发布