当前位置:网站首页>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);
}
边栏推荐
- LeetCode 0640.求解方程:过几天就看不懂了的迷惑性代码,但是是详解
- 机器人控制器编程实践指导书旧版-实践七 无线通信(网络)
- Toronto Research Chemicals霉菌毒素分析丨伏马菌素B2
- R语言创建列表数据(list):根据名称索引列表元素、双方括号访问单个元素、单方括号访问子列表
- 关于奉加微PHY62xx系列如何选型?PHY6222/PHY6212/PHY6252
- 机器人控制器编程实践指导书旧版-实践三 直流电机(执行器)
- Kong自定义插件初体验
- HarmonyOS自动化测试框架—Hypium
- 架构-三层架构:三层架构
- R语言拟合ARIMA模型:使用forecast包中的auto.arima函数自动搜索最佳参数组合、模型阶数(p,d,q)、如果已知阶数则直接使用arima函数构建模型(order参数指定阶数)
猜你喜欢
Toronto Research Chemicals BTK抑制剂丨ACP-5197
redis分布式锁
测试接口出现“data“: “Full authentication is required to access this resource“凭证已过期
CSV(Comma-Separate-Values)逗号分隔值文件
CDH6.3.2之Kerberos安全认证_大数据培训
【FAQ】HarmonyOS ETS如何给组件设置边框
烟雾、空气质量、温湿度…自己徒手做个环境检测设备
机器人控制器编程实践指导书旧版-实践七 无线通信(网络)
欧洲核子研究中心首次在量子机器学习研究中取得实效
迪文发布新款2K高清DGUS智能屏
随机推荐
如何学习性能测试?
Wuling Hongguang MINI EV, the only drawback is safety
机器人控制器编程实践指导书旧版-实践八 机器人综合设计
报告详解影响英特尔10/11/12代酷睿处理器的ÆPIC Leak安全漏洞
Selenium - 如何操作鼠标进行悬停、右击、双击、拖拽?
requires ‘angle‘ attribute to be a multiple of 45
Making Pre-trained Language Models Better Few-Shot Learners
AVFrame相关api内存管理
【HMS core】【FAQ】AR Engine、Analytics Kit、Video Editor Kit、Image Kit、Map Kit典型问题合集2
不能直接在交易所期货开户
软链接、硬链接——ln -s 使用
机器人控制器编程实践指导书旧版-实践七 无线通信(网络)
讯飞翻译机抢镜背后,跨语种沟通迈入全新时代
Allegro软件Shape菜单下的每个命令的含义
背景视频铺满盒子
【FAQ】【Push Kit】推送服务,回执配置一直报错、回执过期修改、怎么删除配置的回执
Selenium - 如何操作下拉框、弹出框、滚动条?
Colocate Join :ClickHouse的一种高性能分布式join查询模型
php7中使用“??”运算符
网络层总结(未完待续)