统计二叉树的结点数(C语言)

jiangzhengdong 2009-10-20 03:35:42
#include<stdio.h>
#include<stdlib.h>
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}Bitnode,*Bitree;
void CreatBintree(Bitree *T)
{
char ch;
scanf("%c",&ch);
if(ch=='0')
*T=NULL;
else
{
*T=(Bitnode *)malloc(sizeof(Bitnode));
(*T)->data=ch;
CreatBintree(&((*T)->lchild));
CreatBintree(&((*T)->rchild));
}
}
int CountBintree(Bitree *p)
{
int i=0;
if(*p)
{
i++;
CountBintree(&((*p)->lchild));
CountBintree(&((*p)->rchild));
}
return i;
}
void main()
{
Bitree root;
int m;
CreatBintree(&root);
m=CountBintree(&root);
printf("%d\n",m);
}
...全文
541 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
soon 2009-10-20
  • 打赏
  • 举报
回复
把二叉树遍历一遍,就可以知道。
phpjspasp 2009-10-20
  • 打赏
  • 举报
回复

unsigned int CountBintree(Bitree *p)
{
unsigned int num = 0;
count(p, &num);
return num;
}

void count(Bitree *p, unsigned int *pnum)
{
if( p != NULL ) ++(*pnum);
else return;
count( p->lchild, pnum);
count( p->rchild, pnum);
return;
}

不知道行不行。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧