结点是什么意思c(C语言编程:如何计算二叉树叶子结点数目?)

众所周知,一棵二叉树,如果每一层的节点数达到最大,就是一棵全二叉树。也就是说,如果一棵二叉树的层数为k,节点总数为(2 k)-1,那么它就是一棵全二叉树。



节点:

在图中是0,1,2~~15。这些被称为节点。

叶节点:

即没有子节点的节点,如图中的8,9 ~ 15。

接下来,我们直接用代码来看看如何计算二叉树的叶节点数(C语言):

# include & ltstdio.h & gt # include & lt;stdlib.h & gt int depth; typedef结构节点{ char数据; struct Node * l child; struct Node * Rchild; }BiTNode,* Bitree void CreateBinTree(bit node * * T) { char ch; ch = getchar(); if(ch = = & # 39;#'){ * T = NULL返回;} * T =(bit node *)malloc(sizeof(bit node)); (* T)-& gt;数据= ch CreateBinTree(&((* T)-& gt;lchild)); CreateBinTree(&((* T)-& gt;rchild)); } Bitree create bintree 2() { bit node * T; char ch; ch = getchar(); if(ch = = & # 39;#'){返回NULL} T =(bit node *)malloc(sizeof(bit node)); T-& gt;数据= ch T-& gt;l child = create bintree 2(); T-& gt;rchild = create bintree 2(); } void PreTree(Bitree T) { if(T = = NULL)return; printf(& # 34;% c & # 34,T-& gt;数据); PreTree(T-& gt;l child); PreTree(T-& gt;rchild); } int posttreedepth(bit node * T)//查找树的深度(后序遍历) { inthl,HR if(T!= NULL) { HL = PostTreeDepth(T-& gt;l child); HR = post tree depth(T-& gt;rchild); max = HL & gt;hr?HL:HR; return(max+1); } 否则返回0; } void preeedepth(bit node * t,int h)//查找树的深度(一阶遍历)深度是一个全局变量 { [/h = NULL] { if(h & gt;depth)深度= h; pretree depth(T-& gt;Lchild,h+1); pretree depth(T-& gt;Rchild,h+1); } } int preorder num(bitree t)//Recursive { int nlef = 0,nright = 0。 if(T==NULL)返回0; if(T-& gt;l child = = NULL & & T-& gt;Rchild==NULL)返回1; if(T-& gt;l child){ nlef = PreOrderNUM(T-& gt;l child); } if(T-& gt;rchild){ n right = PreOrderNUM(T-& gt;rchild); } return nlef+n right; } int main() { bit node * T =(bit node *)malloc(sizeof(bit node)); T = create bintree 2(); //PreTree(T); //printf(& # 34;% d & # 34,post tree depth(T)); printf(& # 34;% d & # 34,PreOrderNUM(T)); 返回0; }作者:MCL

如果不知道怎么问,对于准备学习编程的人来说,如果想更好的提升自己的编程核心能力(内功),不妨现在就开始!

编程学习书籍分享:



编程学习视频分享:

结点是什么意思



整理分享(多年学习的源代码,项目实用视频,项目笔记,基础入门教程)

欢迎转行学编程的伙伴,用更多的素材,比自己学得更快,成长更快!

如果你对C/C++感兴趣,可以关注边肖,后台私信我:【编程通信】我们一起学习吧!可以获取一些关于项目学习的C/C++视频资料!已经设置了关键词自动回复,就自动获取吧!

您可以还会对下面的文章感兴趣

使用微信扫描二维码后

点击右上角发送给好友