求二叉树中叶子数的算法

来源:互联网 发布:php有一对小兔子 编辑:程序博客网 时间:2024/04/27 22:28
//*int num=0;//以二叉链表作为存储结构,试编写求二叉树中叶子数的算法void LeafCount(BitTree T){ if(T){LeafCount(T->lchild) ; //中序遍历左子树if(!T->lchild && !T->rchild) num++; //访问根结点LeafCount(T->rchild) ; //中序遍历右子树}}//*//*void LeafCount_2(BitTree T,int& num){ if(T==NULL)return;if (!T->lchild && !T->rchild) {num+=1;return; }LeafCount_2(T->lchild,num) ; //前序遍历左子树LeafCount_2(T->rchild,num) ; //前序遍历右子树}*/

原创粉丝点击