一.初级思路

拿到题之后的好习惯——先在纸上写思路

1.构建二叉树:

char* BinTreeBuilding(PBinTreeNode ptr_node,char *ptr_str){//创建二叉树 ptr_node->info=*ptr_str;if(*ptr_str!='#'){PBinTreeNode left=CreateBinTreeNode();left->dad=ptr_node;ptr_node->lchild=left;ptr_str++;ptr_str=BinTreeBuilding(left,ptr_str)+1;PBinTreeNode right=CreateBinTreeNode();right->dad=ptr_node;ptr_node->rchild=right;return BinTreeBuilding(right,ptr_str);} else{return ptr_str;}
}

2.计算二叉树叶子结点数目:

void LeavesCounting(PBinTreeNode ptr_node,int* ptr_count){//统计二叉树中叶子结点的数目 if(ptr_node->lchild->info!='#'){LeavesCounting(ptr_node->lchild,ptr_count);}if(ptr_node->rchild->info!='#'){LeavesCounting(ptr_node->rchild,ptr_count);}if(ptr_node->lchild->info=='#'&&ptr_node->rchild->info=='#'){(*ptr_count)++;}
}

最后,源码分享:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef char DataType;
typedef struct BinTreeNode{DataType info;struct BinTreeNode* lchild;struct BinTreeNode* rchild;struct BinTreeNode* dad;
}BinTreeNode,*PBinTreeNode; void InitializeBinTreeNode(PBinTreeNode PNode){//初始化二叉树中的一个结点 PNode->info='0';PNode->lchild=NULL;PNode->rchild=NULL;PNode->dad=NULL;
}PBinTreeNode CreateBinTreeNode(){//创建二叉树中的一个结点 PBinTreeNode PNode=(PBinTreeNode)malloc(sizeof(BinTreeNode));if(PNode==NULL){printf("out of space!");}else{InitializeBinTreeNode(PNode);return PNode;}
}char* BinTreeBuilding(PBinTreeNode ptr_node,char *ptr_str){//创建二叉树 ptr_node->info=*ptr_str;if(*ptr_str!='#'){PBinTreeNode left=CreateBinTreeNode();left->dad=ptr_node;ptr_node->lchild=left;ptr_str++;ptr_str=BinTreeBuilding(left,ptr_str)+1;PBinTreeNode right=CreateBinTreeNode();right->dad=ptr_node;ptr_node->rchild=right;return BinTreeBuilding(right,ptr_str);} else{return ptr_str;}
}void LeavesCounting(PBinTreeNode ptr_node,int* ptr_count){//统计二叉树中叶子结点的数目 if(ptr_node->lchild->info!='#'){LeavesCounting(ptr_node->lchild,ptr_count);}if(ptr_node->rchild->info!='#'){LeavesCounting(ptr_node->rchild,ptr_count);}if(ptr_node->lchild->info=='#'&&ptr_node->rchild->info=='#'){(*ptr_count)++;}
}int main(){char str[1000];scanf("%s",str);char *ptr_str=str;PBinTreeNode ptr_tree=CreateBinTreeNode();BinTreeBuilding(ptr_tree,ptr_str);int leaf_count=0;int *ptr_count=&leaf_count;LeavesCounting(ptr_tree,ptr_count);printf("%d",leaf_count);return 0;
}

推荐大家像我这样,先在纸上把思路都理顺了,最后一步再去上机敲代码,这样子效率会提升很多很多。

二.思路改进

我们改进“二叉树创建函数”:

1.用getchar()代替字符指针ptr_str;

2.将结点的创立放到子函数中完成

3.在结构体struct BinTreeNode的定义中,去掉指向父节点的指针“*dad”。

改进后的“二叉树创建函数”如下所示:

PBinTreeNode BinTreeBuilding(){//这里,用getchar()代替字符指针 char s=getchar();if(s!='#'){PBinTreeNode cur=CreateBinTreeNode();cur->info=s;//将结点的创立下放到子函数中 cur->lchild=BinTreeBuilding();cur->rchild=BinTreeBuilding();//去掉了指向父结点的指针 return cur;}else{return NULL;}
}

我们还要改进“叶子结点统计函数”:

1.我们利用好函数的返回值,而不单单只是利用指针

改进后的“叶子结点统计函数”如下所示:

int LeavesCounting(PBinTreeNode ptr_node){//统计二叉树中叶子结点的数目 if(ptr_node->lchild==NULL&&ptr_node->rchild==NULL){return 1;}else{int n_lchild=0,n_rchild=0;if(ptr_node->lchild!=NULL){n_lchild=LeavesCounting(ptr_node->lchild);}if(ptr_node->rchild!=NULL){n_rchild=LeavesCounting(ptr_node->rchild);}return n_lchild+n_rchild;}
}

改进后完整的源代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef char DataType;
typedef struct BinTreeNode{DataType info;struct BinTreeNode* lchild;struct BinTreeNode* rchild;
}BinTreeNode,*PBinTreeNode; void InitializeBinTreeNode(PBinTreeNode PNode){//初始化二叉树中的一个结点 PNode->info='0';PNode->lchild=NULL;PNode->rchild=NULL;
}PBinTreeNode CreateBinTreeNode(){//创建二叉树中的一个结点 PBinTreeNode PNode=(PBinTreeNode)malloc(sizeof(BinTreeNode));if(PNode==NULL){printf("out of space!");}else{InitializeBinTreeNode(PNode);return PNode;}
}PBinTreeNode BinTreeBuilding(){//这里,用getchar()代替字符指针 char s=getchar();if(s!='#'){PBinTreeNode cur=CreateBinTreeNode();cur->info=s;//将结点的创立下放到子函数中 cur->lchild=BinTreeBuilding();cur->rchild=BinTreeBuilding();//去掉了指向父结点的指针 return cur;}else{return NULL;}
}int LeavesCounting(PBinTreeNode ptr_node){//统计二叉树中叶子结点的数目 if(ptr_node->lchild==NULL&&ptr_node->rchild==NULL){return 1;}else{int n_lchild=0,n_rchild=0;if(ptr_node->lchild!=NULL){n_lchild=LeavesCounting(ptr_node->lchild);}if(ptr_node->rchild!=NULL){n_rchild=LeavesCounting(ptr_node->rchild);}return n_lchild+n_rchild;}
}int main(){PBinTreeNode ptr_node=BinTreeBuilding();int leaf_count=LeavesCounting(ptr_node);printf("%d",leaf_count);return 0;
}

西工大NOJ数据结构理论——016.计算二叉树叶子结点数目(耿6.14)相关推荐

  1. 西工大NOJ数据结构理论——015.建立二叉树的二叉链表存储结构(严6.70)

    我相信,大家都已经了解了这道题的背景,以及明白了我们需要做的事情. 对于这道题的背景,相信大家都熟悉,所以就不说了. 关于我们需要做的事情,大家也已经有了自己的思路.所以,我只在下面简短的写一写我的思 ...

  2. 西工大NOJ数据结构理论——018.建立二叉树的二叉链表(严6.65)

    刚点开这道题以后,我的反应是: 1.打开老师发的PPT:DS-Chap6,然后翻到第70页: 2.跟着图解,自己照着画了三四遍(其实是抄了三四遍: 3.然后自己出了一个比较简单的题: 先序:ABDCE ...

  3. 西工大NOJ数据结构理论——001.顺序表的插入运算(耿2.4)

    #include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct NODE {int n ...

  4. 西工大NOJ数据结构理论——013.以十字链表为存储结构实现矩阵相加(严5.27)

      我第一下拿到这个题目,第一反应就是先定义好数据结构,然后构建好十字链表基础操作的函数,也就是"创插遍历"这些操作.下面是我的定义和函数操作. typedef int ElemT ...

  5. 西工大NOJ数据结构理论——017.输出以二叉树表示的算术表达式(严6.51)

    (17条消息) 『西工大-数据结构-NOJ』 017-输出以二叉树表示的算术表达式(耿6.51) 『西北工业大学』__LanXiu的博客-CSDN博客 上面是我参考的一位学长的博客. 先序建立二叉树, ...

  6. 西工大NOJ数据结构理论——021.逆波兰表达式(严7.38)

    这道题我参考的是(80条消息) 『西工大-数据结构-NOJ』 021-逆波兰表达式(耿7.38) 『西北工业大学』__LanXiu的博客-CSDN博客 (准确来说是快期末考试了,所以各科老师都在疯 狂 ...

  7. 西工大NOJ数据结构理论——007.表达式括号与匹配(严3.19)

    害怕提交次数过多,然后不再跑代码,影响成绩,所以又重新创建了两个号来测这道题. 结果全都正确,但就是一直"WA",然后Debug了近5个小时才把"WA"改成&q ...

  8. 西工大NOJ数据结构理论——010.k阶斐波那契数列(严3.32)

    k阶斐波那契序列定义:第k和k+1项为1,前k - 1项为0,从k项之后每一项都是前k项的和 k=2时,斐波那契序列为:0,1,1,2,3,5,8,13... k=3时,斐波那契序列为:0,0,1,1 ...

  9. 西工大NOJ数据结构理论——019.基于图的深度优先搜索策略(耿7.10)

    临近期末考了,所以思路来不及整理了,但大家还是可以仿照我的上几篇文章,"先动笔,再代码"的思路还是不变的. #include<stdio.h> #include< ...

最新文章

  1. 架构之美(china-pub全国独家首发)
  2. html万年历闹钟怎么取消,万年历如何取消整点报时,他上面有四个键,分...
  3. WireShark过滤器选项
  4. MFC 字符串截取成数组 wcstok
  5. 唯一可译码判断c语言_单片机基础实验数码管原理与C语言
  6. L2-038 病毒溯源 (25 分)-PAT 团体程序设计天梯赛 GPLT
  7. 20175212童皓桢 类定义
  8. 阿里百度腾讯facebookMicrosoftGoogle开源项目汇总
  9. HLS(HTTP Live Streaming)协议浅析
  10. 小白如何进阶学习编程?
  11. 地震速度分析matlab程序,地震波频谱分析。。。。。
  12. SpringBoot Kafka工具类封装
  13. FIT/WeFIT 开发者冯华君采访
  14. 芯片常见的三种封装形式
  15. android 卸载内置app,安卓全机型卸载预装软件
  16. C++初学者必练基础编程题【第一期】
  17. 仓储物流行业英文术语、缩略词
  18. 微信王者有ios的服务器吗,就没有iOS微信区的吗...
  19. IDEA连接数据库踩过的坑之无法连接到数据表
  20. 事业单位计算机专技岗笔试题目,2018年北京昌平区事业单位招聘考试科目及考试内容...

热门文章

  1. 用css做透明效果,CSS实现毛玻璃透明效果
  2. Linux安装LibreOffice
  3. 管理故事:授人以鱼,不如授人以渔
  4. Unity FSM(有限状态机)
  5. CSDN设置头像不显示
  6. spring boot毕业论文管理系统 毕业设计源码030946
  7. 五分钟搭建django-admin-honey简单蜜罐应用
  8. 弘辽科技:淘宝联盟该如何推广
  9. 平头哥面试——数字IC面试流程整理
  10. Python问题:bash: syntax error near unexpected token `newline'