PTA 数据结构与算法题目集(中文)6-11
2018-02-01 21:21
567 查看
6-11 先序输出叶结点(15 分)
本题要求按照先序遍历的顺序输出给定二叉树的叶结点。
函数接口定义:
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) { if(!BT->Left&&!BT->Right) printf(" %c",BT->Data); PreorderPrintLeaves(BT->Left); PreorderPrintLeaves(BT->Right); } }
作者: 陈越
单位: 浙江大学
时间限制: 400ms
内存限制: 64MB
代码长度限制
相关文章推荐
- PTA 数据结构与算法题目集(中文)5-11 关键活动 (30分)
- pta-数据结构与算法题目集(中文)-4-1-单链表逆转
- PTA 数据结构与算法题目集(中文)6-1
- PTA 数据结构与算法题目集(中文)5-7 六度空间 (30分)
- PTA 数据结构与算法题目集(中文)5-9 旅游规划 (25分)
- PTA 数据结构与算法题目集(中文)6-6
- PTA 数据结构与算法题目集(中文)6-2
- PTA 数据结构与算法题目集(中文)6-10
- PTA 数据结构与算法题目集(中文)5-8 哈利·波特的考试 (25分)
- PTA 数据结构与算法题目集(中文)6-5
- pta-数据结构与算法题目集(中文)--4-4 链式表的按序号查找
- PTA 数据结构与算法题目集(中文)5-4 是否同一棵二叉搜索树 (25分)
- pta-数据结构与算法题目集(中文)-4-3 求链式表的表长
- 数据结构与算法题目集(中文)4-11 先序输出叶结点 (15分)
- PTA 数据结构与算法题目集(中文)5-5 堆中的路径 (25分)
- pta-数据结构与算法题目集(中文)-4-2 顺序表操作集
- PTA 数据结构与算法题目集(中文)6-9
- PTA 数据结构与算法题目集 5-1 最大子列和问题
- 数据结构与算法题目集(中文)4-10 二分查找 (20分)
- PTA 数据结构题目(1):最大子列和问题(分而治之、在线处理算法)