求二叉树叶子节点数

来源:互联网 发布:淘宝助理上架 权重 编辑:程序博客网 时间:2024/04/27 17:18
#include<stdio.h>#include<stdlib.h>typedef struct BTNode{    struct BTNode *lchild,*rchild;    int data;}BTNode,*btnode;void createBtree(btnode &T){    int t;    scanf("%d",&t);    if(t==0)        T=NULL;    else    {        T=(btnode)malloc(sizeof(BTNode));        T->data=t;        createBtree(T->lchild);        createBtree(T->rchild);    }}int n=0;void count(btnode T){    if(T)    {        if(T->lchild==NULL&&T->rchild==NULL)        n++;        count(T->lchild);        count(T->rchild);    }}void main(){    int s;    btnode T;    createBtree(T);    count(T);    printf("leaves=%d\n",n);}
0 0
原创粉丝点击