编写函数输出该森林(含树)的各个叶子
注意:答案输出时每个元素后面加一个空格 且空树时输出”NULL”
函数接口定义:
void Print_Leaves_Of_CSTree(CSTree CST);
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
#include <stdio.h>
//头文件包含
#include<iostream>
#include<malloc.h>
#include<stdio.h>
using namespace std;
//函数状态码定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define INFEASIBLE -2
#define NULL 0
typedef int Status;
char s[31];
int cnt=0;
//二叉链表存储结构定义
typedef char TElemType;
typedef struct CSNode
{
TElemType data;
struct CSNode *firstchild;
struct CSNode *nextsibling;
}CSTNode,*CSTree;
Status CreateCSTree(CSTree &CST)
{
TElemType e;
e=s[cnt++];
if(e=='*')
CST=NULL;
else
{
CST=(CSTNode*)malloc(sizeof(CSTNode));
if(!CST)
exit(OVERFLOW);
CST->data=e;
CreateCSTree(CST->firstchild);
CreateCSTree(CST->nextsibling);
}
return OK;
}
//下面是需要实现的函数的声明
void Print_Leaves_Of_CSTree(CSTree CST);
//下面是主函数
int main()
{
CSTree CST;
while(cin>>s)
{
CreateCSTree(CST);
if(!CST)
cout<<"NULL";
else Print_Leaves_Of_CSTree(CST);
cout<<endl;
cnt=0;
}
return 0;
}
/* 请在这里填写答案 */
输入样例:
ABH**CE*FG***D***
*
AB*C*D***
输出样例:
H E G D
NULL
B C D
个人解答
void Print_Leaves_Of_CSTree(CSTree CST) {
if (!CST->firstchild) {
printf("%c ", CST->data);
if (CST->nextsibling) Print_Leaves_Of_CSTree(CST->nextsibling);
} else {
Print_Leaves_Of_CSTree(CST->firstchild);
if (CST->nextsibling) Print_Leaves_Of_CSTree(CST->nextsibling);
}
}