已知后序和中序,求层序序列。
刚开始想模仿先序和中序重建二叉树的代码,使用string存储先序和中序序列,以便使用string的find函数和substr函数。
错误代码:只通过第1、4测试点。
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
using namespace std;
struct TreeNode{
char data;
TreeNode * leftchild;
TreeNode * rightchild;
};
TreeNode * Build(string In,string Post){
if(In.size()==0 || Post.size()==0){
return NULL;
}
else{
char cdata=Post[Post.size()-1];
int pos=In.find(cdata);
TreeNode * NewNode=new TreeNode;
NewNode->data=cdata;
NewNode->leftchild=Build(In.substr(0,pos),Post.substr(0,pos));
NewNode->rightchild=Build(In.substr(pos+1),Post.substr(pos,Post.size()-1-pos));
return NewNode;
}
}
void CxOrder(TreeNode * root){
queue<TreeNode *> myqueue;
vector<char> vec;
if(root!=NULL){
myqueue.push(root);
while(!myqueue.empty()){
TreeNode * nNode=myqueue.front();
myqueue.pop();
vec.push_back(nNode->data);
//printf("%c ",nNode->data);
if(nNode->leftchild!=NULL){
myqueue.push(nNode->leftchild);
}
if(nNode->rightchild!=NULL){
myqueue.push(nNode->rightchild);
}
}
for(int i=0;i<vec.size();i++){
if(i!=0){
printf(" ");
}
printf("%c",vec[i]);
}
}
}
int main(){
int N;
char c;
scanf("%d",&N);
getchar();
string In;
string Post;
for(int i=0;i<N;i++){
scanf("%c",&c);
getchar();
Post.push_back(c);
}
for(int i=0;i<N;i++){
scanf("%c",&c);
getchar();
In.push_back(c);
}
TreeNode * root=Build(In,Post);
CxOrder(root);
getchar();
getchar();
return 0;
}
二叉树结点存储的数据类型改为int,并且在全局使用vector向量存储中序和后序序列。
注意:
NewNode->data=cdata;
int length=Inright-pos;
NewNode->leftchild=Build(Inleft,pos-1,Postleft,Postright-length-1);
NewNode->rightchild=Build(pos+1,Inright,Postright-length,Postright-1);
正确代码:
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
vector<int> In;
vector<int> Post;
struct TreeNode{
int data;
TreeNode * leftchild;
TreeNode * rightchild;
};
TreeNode * Build(int Inleft,int Inright,int Postleft,int Postright){
int pos;
if(Inleft>Inright || Postleft>Postright){
return NULL;
}
else{
int cdata=Post[Postright];
for(int i=Inleft;i<=Inright;i++){
if(cdata==In[i]){
pos=i;
break;
}
}
TreeNode * NewNode=new TreeNode;
NewNode->data=cdata;
int length=Inright-pos;
NewNode->leftchild=Build(Inleft,pos-1,Postleft,Postright-length-1);
NewNode->rightchild=Build(pos+1,Inright,Postright-length,Postright-1);
return NewNode;
}
}
void CxOrder(TreeNode * root){
queue<TreeNode *> myqueue;
vector<int> vec;
if(root!=NULL){
myqueue.push(root);
while(!myqueue.empty()){
TreeNode * nNode=myqueue.front();
myqueue.pop();
vec.push_back(nNode->data);
if(nNode->leftchild!=NULL){
myqueue.push(nNode->leftchild);
}
if(nNode->rightchild!=NULL){
myqueue.push(nNode->rightchild);
}
}
for(int i=0;i<vec.size();i++){
if(i!=0){
printf(" ");
}
printf("%d",vec[i]);
}
printf("\n");
}
}
int main(){
int N;
int c;
scanf("%d",&N);
getchar();
for(int i=0;i<N;i++){
scanf("%d",&c);
getchar();
Post.push_back(c);
}
for(int i=0;i<N;i++){
scanf("%d",&c);
getchar();
In.push_back(c);
}
TreeNode * root=Build(0,In.size()-1,0,Post.size()-1);
CxOrder(root);
getchar();
getchar();
return 0;
}