![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
数据结构关于树的问题题,求C语言代码
编写一个程序,实现由先序序列和中序序列以及由中序序列和后序序列构造一棵二叉树的功能(二叉树中每个结点值为单个字符)。要求以括号表示和凹入表示法输出该二叉树。...
编写一个程序,实现由先序序列和中序序列以及由中序序列和后序序列构造一棵二叉树的功能(二叉树中每个结点值为单个字符)。要求以括号表示和凹入表示法输出该二叉树。
展开
1个回答
展开全部
#include<iostream>
using namespace std;
struct Treel{
int a;
Treel *lp,*rp;
};
int CreatTreel(Treel **h,int n){
(*h)=(Treel*)malloc(sizeof(Treel));
cin>>(*h)->a;
if(n==1){
(*h)->lp=NULL;
(*h)->rp=NULL;
return 1;
}
CreatTreel(&((*h)->lp),n-1);
CreatTreel(&((*h)->rp),n-1);
}
int TrvalTreel(Treel *h){
if(h->lp==NULL){
cout<<h->a;
return 1;
}
if(h->rp==NULL){
cout<<h->a;
return 1;
}
TrvalTreel(h->lp);
TrvalTreel(h->rp);
}
void main(){
Treel *head=(Treel*)malloc(sizeof(Treel));
int n=3;
CreatTreel(&head,n);
TrvalTreel(head);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询