编写一个C++程序,它能根据读入的某二叉树的中序序列和后序序列

来源:百度知道 编辑:UC知道 时间:2024/05/26 11:14:25
编写一个C++程序,它能根据读入的某二叉树的中序序列和后序序列(两个英文字母串,每个串长不大于80,各占一行),构造该二叉树,并输出该二叉树的前序序列、叶的个数、二度结点个数及其从根节点开始的最长路径上的各结点。

//DEV C++

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char data;
int height;
struct node *lchild;
struct node *rchild;
}BTNode;

BTNode create(BTNode *&b,char *s1,char *s2)
{
char *p,s11[1024],s12[1024],s21[1024],s22[1024];
int i;
if(strlen(s2)>0)
{b=(BTNode*)malloc(sizeof(BTNode));
p=s2;
b->data=*(p+strlen(s2)-1);
b->lchild=b->rchild=NULL;
i=0;
while(*s1!=b->data){*(s11+i)=*s1;s1++;i++;}
*(s11+i)='\0';
s1++;i=0;
while(*s1!='\0'){*(s12+i)=*s1;s1++;i++;}
*(s12+i)='\0';
i=0;
while(strchr(s11,*s2)!=NULL){*(s21+i)=*s2;s2++;i++;}
*(s21+i)='\0';
i=0;
while(strchr(s12,*s2)!=NULL){*(s22+i)=*s2;s2++;i++;}