其实这个题不该花费这么长时间的,因为这个题就是一个强行组合,前面已经写过了的!!!自己实在是太大意,对细节处的把握不好!
代码:
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}btnode;
char shuru[55];
int sum;
int cnt;
void juqianxujianshu(btnode *&root)
{
char ch=shuru[cnt];
cnt++;
if(ch==',')
root=NULL;
else
{
root=new btnode;
root->data=ch;
juqianxujianshu(root->lchild);
juqianxujianshu(root->rchild);
}
}
///这个是在网上学到的
//int yezi(btnode *p)
//{
// if(p==NULL)
// return 0;
// if(p->lchild==NULL&&p->rchild==NULL)
// return 1;
// return yezi(p->lchild)+yezi(p->rchild);
//}
///这个很low的是自己想的
void yezi(btnode *p)
{
if(p!=NULL)
{
if(p->lchild==NULL&&p->rchild==NULL)
sum++;
yezi(p->lchild);
yezi(p->rchild);
}
}
int main()
{
///刚开始WA了三发就是因为这里,sum重新赋值为零放错了地方!
//sum=0;
while(cin>>shuru)
{
sum=0;
cnt=0;
btnode *root;
juqianxujianshu(root);
yezi(root);
cout<<sum<<endl;
}
return 0;
}