输入的格式:
输入二叉树的先序序列。
提示:一棵二叉树的先序序列是一个字符串,若字符是‘#’,表示该二叉树是空树。
输出的格式:
输出有两行:
第一行为二叉树的中序遍历序列。
第二行为二叉树的叶子结点个数。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
//------二叉树的孩子-兄弟存储 -------
#define MAX_TREE_SIZE 100
#define OK 1
#define ERROR 0
typedef int Status;
int i=0;
typedef struct CSNode //树的二叉链表(孩子-兄弟)存储表示
{
char data;
struct CSNode *firstchild,*nextsibling; //分别为左孩子和孩子的兄弟
}Tree,*CSTree;
CSTree creatTree() //树的二叉链表(孩子-兄弟)存储表示的输入
{
CSTree T;
char datas;
scanf("%c",&datas);
if (datas == '#')
{
T = NULL;
}
else
{
T = (Tree*)malloc(sizeof(Tree));//分配空间
T->data = datas;//赋值
T->firstchild = creatTree();
T->nextsibling = creatTree();
}
return T;
}
Status count(CSTree T){//计算叶子结点函数
if(T==NULL){
return i;
}
else{
if(T->firstchild==NULL&&T->nextsibling==NULL) i++;
else{
if(T->firstchild==NULL) count(T->nextsibling);
else{
if(T->nextsibling==NULL) count(T->firstchild);
else{
count(T->firstchild);
count(T->nextsibling);
}
}
}
}
}
void putout(CSTree T){//以中序输出的函数
if(T==NULL) return;
else{
putout(T->firstchild);
cout<<T->data;
putout(T->nextsibling);
}
}
int main()
{
CSTree A;
A=creatTree();
cout<<"中序遍历序列:";
putout(A);
cout<<endl<<"叶子结点:";
cout<<count(A);
return 0;
}