PTA二叉树求深度和叶子数
来源:互联网 发布:仙人下凡无人知 生肖 编辑:程序博客网 时间:2024/05/16 17:09
//头文件包含#include<stdlib.h>#include<stdio.h>#include<malloc.h>//函数状态码定义#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -1#define INFEASIBLE -2#define NULL 0typedef int Status;//二叉链表存储结构定义typedef int TElemType;typedef struct BiTNode{ TElemType data; struct BiTNode *lchild, *rchild;} BiTNode, *BiTree;//先序创建二叉树各结点Status CreateBiTree(BiTree &T){ TElemType e; scanf("%d",&e); if(e==0)T=NULL; else{ T=(BiTree)malloc(sizeof(BiTNode)); if(!T)exit(OVERFLOW); T->data=e; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } return OK;}//下面是需要实现的函数的声明int GetDepthOfBiTree ( BiTree T);int LeafCount(BiTree T);//下面是主函数int main(){ BiTree T; int depth, numberOfLeaves; CreateBiTree(T); depth= GetDepthOfBiTree(T); numberOfLeaves=LeafCount(T); printf("%d %d\n",depth,numberOfLeaves);}/* 请在这里填写答案 */int GetDepthOfBiTree(BiTree T){ int d=0,d1=0,d2=0; if(T==NULL) d=0; else{ d1=GetDepthOfBiTree(T->lchild); d2=GetDepthOfBiTree(T->rchild); if(d1>d2) d=d1+1; else d=d2+1; } return d;}int LeafCount(BiTree T){ int l=0,l1=0,l2=0; if(T==NULL) l=0; else if(T!=NULL&&T->lchild==NULL&&T->rchild==NULL) l=1; else{ l1=LeafCount(T->lchild); l2=LeafCount(T->rchild); l=l1+l2; } return l;}
阅读全文
0 0
- PTA二叉树求深度和叶子数
- 【算法导论】求二叉树的叶子数和深度
- 求二叉树的深度、宽度和叶子结点数
- 6-4 二叉树求深度和叶子数
- 二叉树求深度和叶子数(20 分)
- 输入二叉树,求父,左,右结点和深度,叶子数
- 二叉树的字符串创建和遍历,求深度,叶子节点数
- 二叉树求深度和宽度,叶子节点数,总结点数
- 6-4 二叉树求深度和叶子数(20 分)
- 二叉树求叶子数
- 二叉树的基本操作(创建、递归和非递归遍历、求深度、求叶子数)
- 二叉树的深度和叶子节点数
- 数据结构树求深度和叶子节点数
- 二叉树 (求叶子结点&深度)
- 二叉树(求深度、叶子结点)
- 树 递归 数叶子 求深度
- 求二叉树非叶子节点数
- 求二叉树的叶子结点数
- 贵有恒,若事事勤思善问,何患不一日千里?--惭愧
- 前端基础-09-form表单&json数据
- osgEarth基础入门
- 杂花生树(四十九)
- 详解sys_open
- PTA二叉树求深度和叶子数
- javaweb前端表单请求路径要和后端web.xml文件配置一定要一致
- FFmpeg介绍
- BaseWeb04-JSP
- 数据链路层的功能
- CentOS SVN搭建 (YUM安装)
- [学习笔记]Python_os模块
- 机房收费系统前期配置重温
- Hibernate的SessionFactory和Session