A1020
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node* lchild;
node* rchild;
};
int post[31],in[31];
int n;
//已知后序遍历和中序遍历创建二叉树
node* create(int posl,int posr,int inl,int inr){
if(posl>posr){
return NULL;
}
node* root=new node;
root->data=post[posr];
int k;
for(k=inl;k<=inr;k++){
if(in[k]==post[posr]){
break;
}
}
int numleft=k-inl;//左子树的结点个数
//左子树的后序区间为[posl,posl+numleft-1]中序区间为[inl,k-1]
//右子树的后序区间为[posl+numleft,posr-1]中序区间为[k+1,inr]
root->lchild=create(posl,posl+numleft-1,inl,k-1);
root->rchild=create(posl+numleft,posr-1,k+1,inr);
return root;
}
//层序遍历
int num=0;
void bfs(node* root){
queue<node*> q;
q.push(root);
while(!q.empty()){
node* now=q.front();
q.pop();
printf("%d",now->data);
num++;
if(num<n) printf(" ");
if(now->lchild!=NULL) q.push(now->lchild);
if(now->rchild!=NULL) q.push(now->rchild);
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)//先输入后序遍历的结果
{
scanf("%d",&post[i]);
}
for(int i=1;i<=n;i++)//再输入中序遍历的结果
{
scanf("%d",&in[i]);
}
node* root=create(1,n,1,n);
bfs(root);//层序遍历
return 0;
}
A1086
node* root=create(0,n-1,0,n-1);
这里注意,这里写成(1,n,1,n)输出错误,以后还是写成(0,n-1,0,n-1)
//已知先序序列和中序序列求后序序列
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node* lchild;
node* rchild;
};
int n;
int pre[31],in[31],post[31];
//创建二叉树
node* create(int prel,int prer,int inl,int inr)
{
if(prel>prer)
{
return NULL;
}
node* root=new node;
root->data=pre[prel];
int k;
for(k=inl;k<=inr;k++)
{
if(in[k]==pre[prel])
{
break;
}
}
int numleft=k-inl;
root->lchild=create(prel+1,prel+numleft,inl,k-1);
root->rchild=create(prel+numleft+1,prer,k+1,inr);
return root;//返回根结点地址
}
//求后序序列
int num=0;
void postorder(node* root)
{
if(root==NULL)
{
return;
}
postorder(root->lchild);
postorder(root->rchild);
printf("%d",root->data);
num++;
if(num<n) printf(" ");
}
int main()
{
char str[5];
scanf("%d",&n);
stack<int> st;
int x,preindex=0,inindex=0;
for(int i=0;i<2*n;i++)
{
scanf("%s",str);
if(strcmp(str,"Push")==0)//入栈
{
scanf("%d",&x);
pre[preindex++]=x;
st.push(x);
}
else
{
in[inindex++]=st.top();
st.pop();
}
}
node* root=create(0,n-1,0,n-1);
postorder(root);
return 0;
}
A1102
#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
struct node{
int lchild,rchild;
}Node[maxn];
bool notroot[maxn]={false};
int n,num=0;//n为结点个数,num为当前已经输出的结点个数
void printf(int id){
printf("%d",id);
num++;
if(num<n) printf(" ");
else printf("\n");
}
//中序遍历输出
void inorder(int root){
if(root==-1){
return;
}
inorder(Node[root].lchild);
printf(root);
inorder(Node[root].rchild);
}
//层序遍历输出
void bfs(int root){
queue<int> q;
q.push(root);
while(!q.empty()){
int now=q.front();
q.pop();
printf(now);
if(Node[now].lchild!=-1) q.push(Node[now].lchild);
if(Node[now].rchild!=-1) q.push(Node[now].rchild);
}
}
//后序遍历,用以反转二叉树
void postorder(int root){
if(root==-1){
return;
}
postorder(Node[root].lchild);
postorder(Node[root].rchild);
swap(Node[root].lchild,Node[root].rchild);//交换左右孩子结点
}
//将输入的字符转换为-1或结点编号
int strtonum(char c){
if(c=='-') return -1;
else{
notroot[c-'0']=true;
return c-'0';
}
}
int findroot(){
for(int i=0;i<n;i++){
if(notroot[i]==false){
return i;
}
}
}
int main()
{
char lchild,rchild;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%*c%c %c",&lchild,&rchild);
Node[i].lchild=strtonum(lchild);
Node[i].rchild=strtonum(rchild);
}
int root=findroot();
postorder(root);
bfs(root);
num=0;
inorder(root);
return 0;
}