统计叶子数.txt
来自「数据结构C实现对二叉树的操作」· 文本 代码 · 共 42 行
TXT
42 行
#include <alloc.h>
#include <stdio.h>
typedef struct node
{ char data;
struct node *lchild,*rchild;
}JD;
void countleaf(JD *bt,int *count)//先序统计叶子数
{ if(bt!=NULL)
{ if((bt->lchild==NULL)&&(bt->rchild==NULL))
{ (*count)++;
return;
}
countleaf(bt->lchild,count);
countleaf(bt->rchild,count);
}
}
JD *crt_bt_pre(JD *bt)//先序建立二叉树
{ char ch;
printf("ch=");
scanf("%c",&ch);
if(ch==' ') bt=NULL;
else
{ bt=(JD *)malloc(sizeof(JD));
bt->data=ch;
bt->lchild=crt_bt_pre(bt->lchild);
bt->rchild=crt_bt_pre(bt->rchild);
}
return(bt);
}
void main()
{ /* ABC00DE0G00F000 */
JD *head=NULL;
int count=0;
head=crt_bt_pre(head);
countleaf(head,&count);
printf("count of leaf node is %d\n",count);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?