当前位置:网站首页>6-11 Preorder output leaf nodes (15 points)
6-11 Preorder output leaf nodes (15 points)
2022-08-10 18:18:00 【jie3606】
本题要求按照先序遍历的顺序输出给定二叉树的叶结点.
函数接口定义:
void PreorderPrintLeaves( BinTree BT );
其中BinTree结构定义如下:
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
函数PreorderPrintLeaves应按照先序遍历的顺序输出给定二叉树BT的叶结点,格式为一个空格跟着一个字符.
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
BinTree CreatBinTree(); /* 实现细节忽略 */
void PreorderPrintLeaves( BinTree BT );
int main()
{
BinTree BT = CreatBinTree();
printf("Leaf nodes are:");
PreorderPrintLeaves(BT);
printf("\n");
return 0;
}
/* 你的代码将被嵌在这里 */
输出样例(对于图中给出的树):
Leaf nodes are: D E H I
解答
void PreorderPrintLeaves(BinTree BT){
if(BT == NULL) return;
if(BT->Left == NULL && BT->Right == NULL)
printf(" %c",BT->Data);
PreorderPrintLeaves(BT->Left);
PreorderPrintLeaves(BT->Right);
}
完整的代码
完整的代码
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
BinTree CreatBinTree(); /* 实现细节忽略 */
void PreorderPrintLeaves( BinTree BT );
int main()
{
BinTree BT = CreatBinTree();
printf("Leaf nodes are:");
PreorderPrintLeaves(BT);
printf("\n");
return 0;
}
BinTree CreatBinTree() {
ElementType dada;
scanf("%c",&dada);
if(dada =='-'){
return NULL;
}
BinTree root = (BinTree) malloc(sizeof (struct TNode));
root->Data = dada;
root->Left = CreatBinTree();
root->Right = CreatBinTree();
return root;
}
void PreorderPrintLeaves(BinTree BT){
if(BT == NULL) return;
if(BT->Left == NULL && BT->Right == NULL)
printf(" %c",BT->Data);
PreorderPrintLeaves(BT->Left);
PreorderPrintLeaves(BT->Right);
}
边栏推荐
- Toronto Research Chemicals农药检测丨Naled-d6
- Mysql索引、事务与存储引擎
- 期货开户手续费加1分已经是常态
- 迪文发布新款2K高清DGUS智能屏
- H3C_堆叠(IRF)及链路聚合在项目中的综合应用
- 产品说明丨Android端使用MobPush快速集成方法
- 函数柯里化(curry)
- Live Review|How to build an enterprise-level cloud management platform in the multi-cloud era?(with the download of the construction guide)
- 去除富文本标签样式
- 基于AWS构建云上数仓第二步:AWS常见服务简介
猜你喜欢
随机推荐
unity 坑坑001
Toronto Research Chemicals BTK抑制剂丨ACP-5197
装饰者模式
【数据存储精讲】整型和浮点型有什么区别?为什么会精度丢失?
Colocate Join :ClickHouse的一种高性能分布式join查询模型
AVFrame相关api内存管理
想玩转监控神器Prometheus吗?
R语言使用ggpubr包的ggsummarystats函数可视化箱图(通过ggfunc参数设置)、在可视化图像的下方添加描述性统计结果表格、设置add参数为jitter添加抖动数据点
hping3的使用
zabbix配置触发器
瑞虎8 PRO产品性能分析,一文读懂究竟何为“全域动力科技旗舰”
「业务架构」业务能力的热图是什么,有啥用?
【FAQ】HarmonyOS ETS如何给组件设置边框
如何学习性能测试?
php7中使用“??”运算符
Kong自定义插件初体验
awk if else if else
MySQL数据高级查询之连接查询、联合查询、子查询[通俗易懂]
FFmpeg花屏解决(修改源码,丢弃不完整帧)
设置iptables规则来保护CS服务器









