【数据结构基础】求二叉树叶子结点

来源:互联网 发布:python 移除模块 编辑:程序博客网 时间:2024/04/27 15:48
#include<stdio.h>#include<stdlib.h>static  int count=0;typedef struct TreeNode{    char element;    struct TreeNode *left,*right;}Tree,*BTree;BTree BuildTree(void){    BTree T;    char ch;    ch=getchar();    if(ch=='#'){        T=NULL;    }else{        T=(BTree)malloc(sizeof(Tree));        T->element=ch;        T->left=BuildTree();        T->right=BuildTree();    }    return T;}int CountLeaf(BTree T){    if(T){        if(!T->left && !T->right){            printf("%c",T->element);            count++;        }        CountLeaf(T->left);        CountLeaf(T->right);     }   } int main(void){    BTree T;    int num;    T=BuildTree();    CountLeaf(T);    printf("\n%d\n",count);    return 0;} 

这里写图片描述

0 0