实现由先序、中序序列构造二叉树的算法,实现由后序、中序序列构造二叉树的算法
一道上机题 思想是递归
如果不理解原理可以看这个博客 注释里的说明很详细:http://blog.csdn.net/yunzhongguwu005/article/details/9270085
在代码的注释部分有测试样例
先序和中序:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<stack>
#include<queue>
#include<cmath>
#include<stack>
#include<list>
#include<map>
#include<set>
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define gi(x) scanf("%d",&x)
#define gi2(x,y) scanf("%d%d",&x,&y)
#define gll(x) scanf("%lld",&x)
#define gll2(x,y) scanf("%lld%lld",&x,&y)
#define gc(x) scanf("%c",&x)
#define gc2(x,y) scanf("%c%c",&x,&y)
using namespace std;
const double eps=1e-8;
typedef long long ll;
const int MAXN=100005;
const int mod=1000000007;
const ll llinf = (ll)(1e18) + 500;
const int inf=0x3f3f3f3f;
struct Node{
char c;
Node *lson;
Node *rson;
};
void build(Node* &t,string prestr,string instr){
if(prestr.size()==0){
t=NULL;
return;
}
char rootch=prestr[0];
int index=instr.find(rootch);//下标
string lsoninstr=instr.substr(0,index);
string rsoninstr=instr.substr(index+1);
int lsonlen=lsoninstr.size();
//int rsonlen=rsoninstr.size();
string lsonprestr=prestr.substr(1,lsonlen);
string rsonprestr=prestr.substr(lsonlen+1);
t=new Node;
if(t){
t->c=rootch;
build(t->lson, lsonprestr, lsoninstr);
build(t->rson, rsonprestr, rsoninstr);
}
}
void preorderTraverse(Node* & t)
{
if(t!=NULL)
{
cout<<t->c;
preorderTraverse(t->lson);
preorderTraverse(t->rson);
}
}
void inorderTraverse(Node* & t)
{
if(t!=NULL)
{
inorderTraverse(t->lson);
cout<<t->c;
inorderTraverse(t->rson);
}
}
void postorderTraverse(Node* & t)
{
if(t!=NULL)
{
postorderTraverse(t->lson);
postorderTraverse(t->rson);
cout<<t->c;
}
}
//string pre = "1234567";
//string in = "3241657";
//后序 3 4 2 6 7 5 1
int main(){
int i,j,k;
string prestr,instr;
// prestr="ABCDEFG";
// instr="CBEDAFG";
prestr="1234567";
instr="3241657";
Node *root;
build(root,prestr,instr);
preorderTraverse(root);
cout<<endl;
inorderTraverse(root);
cout<<endl;
postorderTraverse(root);
cout<<endl;
return 0;
}
中序和后序:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<stack>
#include<queue>
#include<cmath>
#include<stack>
#include<list>
#include<map>
#include<set>
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define gi(x) scanf("%d",&x)
#define gi2(x,y) scanf("%d%d",&x,&y)
#define gll(x) scanf("%lld",&x)
#define gll2(x,y) scanf("%lld%lld",&x,&y)
#define gc(x) scanf("%c",&x)
#define gc2(x,y) scanf("%c%c",&x,&y)
using namespace std;
const double eps=1e-8;
typedef long long ll;
const int MAXN=100005;
const int mod=1000000007;
const ll llinf = (ll)(1e18) + 500;
const int inf=0x3f3f3f3f;
struct Node{
char c;
Node *lson;
Node *rson;
};
void build(Node* &t,string poststr,string instr){
if(poststr.size()==0){
t=NULL;
return;
}
char rootch=poststr[poststr.size()-1];
int index=instr.find(rootch);//下标
string lsoninstr=instr.substr(0,index);
string rsoninstr=instr.substr(index+1);
int lsonlen=lsoninstr.size();
int rsonlen=rsoninstr.size();
string lsonpoststr=poststr.substr(0,lsonlen);
string rsonpoststr=poststr.substr(lsonlen,poststr.size()-1-lsonlen);
//string rsonpoststr=rsonpoststrtmp.substr(0,);
t=new Node;
if(t){
t->c=rootch;
build(t->lson, lsonpoststr, lsoninstr);
build(t->rson, rsonpoststr, rsoninstr);
}
}
void preorderTraverse(Node* & t)
{
if(t!=NULL)
{
cout<<t->c;
preorderTraverse(t->lson);
preorderTraverse(t->rson);
}
}
void inorderTraverse(Node* & t)
{
if(t!=NULL)
{
inorderTraverse(t->lson);
cout<<t->c;
inorderTraverse(t->rson);
}
}
void postorderTraverse(Node* & t)
{
if(t!=NULL)
{
postorderTraverse(t->lson);
postorderTraverse(t->rson);
cout<<t->c;
}
}
//string pre = "1234567";
//string in = "3241657";
//后序 3 4 2 6 7 5 1
int main(){
int i,j,k;
string poststr,instr;
// prestr="ABCDEFG";
// instr="CBEDAFG";
poststr="3426751";
instr="3241657";
Node *root;
build(root,poststr,instr);
preorderTraverse(root);
cout<<endl;
inorderTraverse(root);
cout<<endl;
postorderTraverse(root);
cout<<endl;
return 0;
}
---------------------
版权声明:本文为CSDN博主「Frozensmile」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/frozensmile/article/details/78530305

浙公网安备 33010602011771号