求叶子结点个数

来源:互联网 发布:linux mutex 编辑:程序博客网 时间:2024/06/11 04:57
#include<iostream>using namespace std;typedef struct BiTNode{char data;struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;void CreateBiTree(BiTree &T){char ch;cin>>ch;if(ch=='#') T=NULL;else{T=new BiTNode;T->data=ch;CreateBiTree(T->lchild);CreateBiTree(T->rchild);}}int LeafNodeCount(BiTree &T){if(T==NULL)return 0;else if(T->lchild==NULL&&T->rchild==NULL)return 1;else return LeafNodeCount(T->lchild)+LeafNodeCount(T->rchild);}int main(){BiTree tree;cout<<"请输入建立二叉链表的序列:";CreateBiTree(tree);cout<<"叶子结点的个数为:"<<LeafNodeCount(tree)<<endl; }

0 0