代码测试:
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct Bitnode{ int data; struct Bitnode *lchild,*rchild; }Bitnode; Bitnode *a[100010]; Bitnode* CreatBitree_level(){ Bitnode* t=(Bitnode*)malloc(sizeof(Bitnode)); int i=0,j=0; a[i++]=t; int n; while(scanf("%d",&n),n!=-1){ Bitnode* p=a[j++]; p->data=n; p->lchild=(Bitnode*)malloc(sizeof(Bitnode)); p->rchild=(Bitnode*)malloc(sizeof(Bitnode)); p->lchild->data=0; p->rchild->data=0; a[i++]=p->lchild; a[i++]=p->rchild; } return t; } int leafs(Bitnode* t){ if(t->data==0) return 0; else{ if(t->lchild->data==0&&t->rchild->data==0) return 1; else return leafs(t->lchild)+leafs(t->rchild); } } int main(){ Bitnode *t; int n; scanf("%d",&n); while(n--){ t=CreatBitree_level(); printf("%d\n",leafs(t)); } }