6_42_二叉树递归求叶子节点个数
#include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef struct node { int data; struct node*lchild,*rchild; }tnode,*tree; tree creat() { int x; tree t; scanf("%d",&x); if(x==0)t=NULL; else { t=(tnode*)malloc(sizeof(tnode)); t->data=x; t->lchild=creat(); t->rchild=creat(); } return t; } int LeafNum(tree t) { if(t) { if(!t->lchild&&!t->rchild) return 1; return LeafNum(t->lchild)+LeafNum(t->rchild); } return 0; } int main() { tree t=creat(); printf("%d\n",LeafNum(t)); }
版权声明:本文为博主原创文章,未经博主允许不得转载。