(已知二叉树的中后,先中序两序遍历建树)

L2-006 树的遍历

题目链接

L2-006 树的遍历 (25 分)
 

给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:

输入第一行给出一个正整数N(≤),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:

在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

输出样例:

4 1 6 3 5 7 2


解题思路:我们可以知道后序遍历的最后一位为根节点,然后可以根据这个根结点在中序遍历找到左子树,右子树的大小,以此反复,可以建成该二叉树。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define ri register int
typedef long long ll;
int l[50],r[50];
vector<int> ans;
int x_order[50],z_order[50],h_order[50];//先序遍历 中序遍历   后序遍历 
int build(int lm1,int rm1,int lm2,int rm2){//后序  中序 
	if(lm1>rm1)return 0;
	int root=h_order[rm1]; 
	int be=lm2;
	int cnt=0;//左子树节点数 
	while(z_order[be]!=root)be++,cnt++;
	l[root]=build(lm1,lm1+cnt-1,lm2,be-1);
	r[root]=build(lm1+cnt,rm1-1,be+1,rm2);
	return root;
}
queue<int> que;
void bfs(){
	int tem;
	while(!que.empty()){
		tem=que.front();
		que.pop();
		ans.push_back(tem);
		if(l[tem]!=0){
			que.push(l[tem]);
		}
		if(r[tem]!=0){
			que.push(r[tem]);
		}
	}
}
int main() {
	int n;
	scanf("%d",&n); 
	for(int i=0;i<n;i++){
		scanf("%d",&h_order[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&z_order[i]);
	}
	int root=build(0,n-1,0,n-1);
//	cout<<root<<endl;
	que.push(root);
	bfs();
	int len=ans.size();
	for(int i=0;i<len;i++){
		if(i!=(len-1)){
			cout<<ans[i]<<" ";
		}
		else{
			cout<<ans[i];
		}
	}
	return 0;
}

  L2-011 玩转二叉树

题目链接

给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。

输入格式:

输入第一行给出一个正整数N≤30),是二叉树中结点的个数。第二行给出其中序遍历序列。第三行给出其前序遍历序列。数字间以空格分隔。

输出格式:

在一行中输出该树反转后的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:

7
1 2 3 4 5 6 7
4 1 3 2 6 5 7

输出样例:

4 6 1 7 5 3 2

思路:和上道题题一样,先找到根节点,然后确定左子树,右子树的大小。这里说一下:所谓镜面反射,层次遍历我们可以先访问右子树,然后访问左子树,即可。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define ri register int
typedef long long ll;
int l[50],r[50];
vector<int> ans;
int x_order[50],z_order[50],h_order[50];//先序遍历 中序遍历   后序遍历 
int build(int lm1,int rm1,int lm2,int rm2){//先序  中序 
    if(lm1>rm1)return 0;
    int root=x_order[lm1]; 
    int be=lm2;
    int cnt=0;//左子树节点数 
    while(z_order[be]!=root)be++,cnt++;
    l[root]=build(lm1+1,lm1+cnt,lm2,be-1);
    r[root]=build(lm1+cnt+1,rm1,be+1,rm2);
    return root;
}
queue<int> que;
void bfs(){
    int tem;
    while(!que.empty()){
        tem=que.front();
        que.pop();
        ans.push_back(tem);
        if(r[tem]!=0){
            que.push(r[tem]);
        }
        if(l[tem]!=0){
            que.push(l[tem]);
        }
    }
}
int main() {
    int n;
    scanf("%d",&n); 
    for(int i=0;i<n;i++){
        scanf("%d",&z_order[i]);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&x_order[i]);
    }
    int root=build(0,n-1,0,n-1);
    que.push(root);
    bfs();
    int len=ans.size();
    for(int i=0;i<len;i++){
        if(i!=(len-1)){
            cout<<ans[i]<<" ";
        }
        else{
            cout<<ans[i];
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Zhi-71/p/10624894.html