已知先序遍历求叶子数
#include #include <bits/stdc++.h> using namespace std; char a[55]; int s=0; int sum; struct node { char data; struct node *l,*r; }; struct node *creat() { struct node root; char c; c=a[s++]; if(c==’,’) root=NULL; else { root=(struct node )malloc(sizeof(struct node)); root->data=c; if(root) { root->l=creat(); root->r=creat(); } } return root; }; int yezi(struct node *root) { if(root NULL) return 0; if(root->l NULL&&root->r==NULL) return 1; else return yezi(root->l)+yezi(root->r); } int main() { struct node *root; while(~scanf("%s",a)) { s=0; root=creat(); sum=yezi(root); printf("%d\n",sum); }