当前位置:网站首页>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);
}
边栏推荐
- 【测试】黑盒测试用例设计方法
- 机器人控制器编程实践指导书旧版-实践六 LCD液晶显示(点阵)
- 【HMS core】【FAQ】Analytics Kit、Push Kit典型问题合集3
- Toronto Research Chemicals农药检测丨Naled-d6
- 多线程与高并发(11)——经典面试题之实现一个容器,提供两个方法,add,size。
- Allegro软件Shape菜单下的每个命令的含义
- 五菱宏光MINI EV,唯一的缺点就是安全性
- 【图像分割】基于元胞自动机实现图像分割附matlab代码
- 机器人控制器编程实践指导书旧版-实践四 步进电机(执行器)
- 【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
猜你喜欢
随机推荐
AVFrame相关api内存管理
海思HI3516DV300开发资料
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辐射的光谱——
记录某博彩网站渗透
Toronto Research Chemicals BTK抑制剂丨ACP-5197
R语言使用oneway.test函数执行单因素方差分析(One-Way ANOVA)、使用数据集的子集数据进行单因素方差分析(subset函数筛选数据子集)
WebRTC source code analysis nack detailed explanation
Before opening a futures account, you must confirm the handling fee as soon as possible
R语言检验时间序列的平稳性:使用fUnitRoots包中的adfTest函数检验时间序列数据是否具有平稳性(设置参数type为nc时、既不去除趋势也不进行中心化处理)
产品-Axure9英文版,A页面内a1状态跳转B页面的b2状态,(条件跳转状态)
同一块中出现两个 * 就不能正常显示
多线程与高并发(11)——经典面试题之实现一个容器,提供两个方法,add,size。
「NewSQL技术」Greenplum 6中的OLTP负载性能提升60倍以上
关于奉加微PHY62xx系列如何选型?PHY6222/PHY6212/PHY6252
requires ‘angle‘ attribute to be a multiple of 45
【图像去雾】基于颜色衰减先验的图像去雾附matlab代码
img转base64
如何学习性能测试?
c语言进阶篇:柔性数组