当前位置:网站首页>6-11 先序输出叶结点(15分)
6-11 先序输出叶结点(15分)
2022-08-10 17:54: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);
}
边栏推荐
- Wuling Hongguang MINI EV, the only drawback is safety
- Before opening a futures account, you must confirm the handling fee as soon as possible
- 忍不住 - 发个新帖子【为什么把红圈的功能入口隐藏?需要移动到鼠标到位置驻停才显示?】- 请投票
- 机器人控制器编程实践指导书旧版-实践六 LCD液晶显示(点阵)
- R语言检验时间序列的平稳性:使用fUnitRoots包中的adfTest函数检验时间序列数据是否具有平稳性(设置参数type为nc时、既不去除趋势也不进行中心化处理)
- flex使用align-content无效
- FFmpeg Huaping solution (modify source code, discard incomplete frames)
- Active users of mobile banking grew rapidly in June, hitting a half-year high
- 「业务架构」业务能力的热图是什么,有啥用?
- R语言拟合ARIMA模型:使用forecast包中的auto.arima函数自动搜索最佳参数组合、模型阶数(p,d,q)、如果已知阶数则直接使用arima函数构建模型(order参数指定阶数)
猜你喜欢

zabbix配置触发器

中国芯片的营收首破万亿,优势凸显的成熟工艺产能将称霸全球

烟雾、空气质量、温湿度…自己徒手做个环境检测设备

【FAQ】【Push Kit】推送服务,回执配置一直报错、回执过期修改、怎么删除配置的回执

【FAQ】HarmonyOS ETS如何给组件设置边框

【FAQ】【Push Kit】 华为怎么设置角标

三坐标雷达显示软件 SPx Viewer-3D

开发模式对测试的影响

产品说明丨Android端使用MobPush快速集成方法

Before opening a futures account, you must confirm the handling fee as soon as possible
随机推荐
讯飞翻译机抢镜背后,跨语种沟通迈入全新时代
「企业架构」什么是Zachman框架?
leet面试150
企业如何通过北森HR SaaS 自动化管理员工账号生命周期
最新手机号码的正则
文档标题能否支持公式
机器人控制器编程实践指导书旧版-实践七 无线通信(网络)
【图像分割】基于元胞自动机实现图像分割附matlab代码
组合模式
pip安装时 fatal error C1083 无法打开包括文件 “io.h” No such file or directory
【接入指南 之 直接接入】手把手教你快速上手接入HONOR Connect平台(下)
Go 语言快速入门指南:第四篇 与数据为舞之数组
网络可观测性:让您的网络监控更上一层楼|TechGenix
【FAQ】【Push Kit】推送服务,回执配置一直报错、回执过期修改、怎么删除配置的回执
php7中使用“??”运算符
eager模式和graph模式 Tensorflow
哈夫曼实现文件压缩解压缩(c语言)
D-Wave成功上市!量子计算商业化正在加速
Word里表格跨页时自动断开,表格后留有空白部分,未布满整页,如何操作让表格上下页均匀布满?
AVFrame related api memory management