二叉树的程序疑惑
#include<iostream.h>#include<stdlib.h>
typedef struct Node
{
char ch;
char data;
struct Node *lchild;
struct Node *rchild;
}BiTNode,*BiTree;
void CreateBitree(BiTree *root)
{
char ch;
cin>>ch;
if(ch=='#')
*root=NULL;
else
{
*root=(BiTNode *)malloc(sizeof(BiTNode));
(*root)->data=ch;
CreateBitree(&((*root)->lchild));
CreateBitree(&((*root)->rchild));
}
}
int depth(BiTree root)
{
int ldepth,rdepth;
if(!root)
return 0;
else
{
ldepth=depth(root->lchild);
rdepth=depth(root->rchild);
if(ldepth>rdepth)
return ldepth+1;
else
return rdepth+1;
}
return 1;
}
void countleaf(BiTree bt,int *c)
{
if(bt)
{
if(bt->lchild==NULL&&bt->rchild==NULL)
{
*c=*c+1;
}
countleaf(bt->lchild,c);
countleaf(bt->rchild,c);
}
}
void main()
{
BiTree bt;
CreateBitree(&bt);
cout<<"树的深度: "<<depth(bt)<<endl;
int c=0;
countleaf(bt,&c);
cout<<"叶子结点个数: "<<c<<endl;
}
在上述程序中,为什么树的深度和叶子结点个数求不对?请高手指点一下!