当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
Making Pre-trained Language Models Better Few-Shot Learners
如何学习性能测试?
【深度学习21天学习挑战赛】4、初尝循环神经网络(RNN)——股票预测
报告详解影响英特尔10/11/12代酷睿处理器的ÆPIC Leak安全漏洞
三星Galaxy Watch5产品图片流出 非Pro表款亦有蓝宝石加持
Toronto Research Chemicals霉菌毒素分析丨T2 四醇
Live Review|How to build an enterprise-level cloud management platform in the multi-cloud era?(with the download of the construction guide)
欧洲核子研究中心首次在量子机器学习研究中取得实效
【2015】【论文笔记】等离子光混合器THz辐射的光谱——
const的自己理解
随机推荐
640. 求解方程
pip安装时 fatal error C1083 无法打开包括文件 “io.h” No such file or directory
oracle11g体系结构
EasyGBS连接mysql数据库提示“can’t connect to mysql server”,如何解决?
Wuling Hongguang MINI EV, the only drawback is safety
FFmpeg花屏解决(修改源码,丢弃不完整帧)
设置iptables规则来保护CS服务器
LeetCode 0640.求解方程:过几天就看不懂了的迷惑性代码,但是是详解
开发模式对测试的影响
【HMS core】【FAQ】Analytics Kit、Push Kit典型问题合集3
unity 坑坑001
运维如何学习、自我提升价值?
R语言使用ggpubr包的ggbarplot函数可视化柱状图、设置add参数为mean_se和jitter可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)和抖动数据点分布
R语言使用oneway.test函数执行单因素方差分析(One-Way ANOVA)、使用数据集的子集数据进行单因素方差分析(subset函数筛选数据子集)
AVFrame相关api内存管理
定时器循环展示数组
【HMS core】【FAQ】AR Engine、Analytics Kit、Video Editor Kit、Image Kit、Map Kit典型问题合集2
go语言的性能基准测试、性能优化测试和性能调优
「业务架构」业务能力的热图是什么,有啥用?
CSV(Comma-Separate-Values)逗号分隔值文件