Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
题意:给出该二叉树有多少个结点,后面的第一个序列是后序遍历序列,第二个是中序遍历序列,根据这两个序列求层次遍历序列。
思路:根据两个遍历序列构造二叉树,然后用层序遍历(广度优先搜索--分岔口总是先访问可以直接到达的结点(将结点的左右孩子先放入队列内))求出层次遍历序列
代码:
#include<iostream>
#include<queue>
using namespace std;
const int maxn=50;
int in[maxn],post[maxn];
int n;
struct node{
int data;
node* lchild;
node* rchild;
};
//根据序列构造二叉树;
node* create(int postL,int postR,int inL,int inR){
if(postL>postR)return NULL;//递归边界;
node* root=new node;//构造根节点;
root->data=post[postR];
int k;
for(k=inL;k<=inR;k++){//注意这里是inL到inR,在子树内寻找跟根节点,不是从0到inR,还有因为传入的是0到n-1,所以是可以等于inR的;
if(in[k]==root->data){
break;
}
}
int leftnum=k-inL;//确认左右子树;
root->lchild=create(postL,postL+leftnum-1,inL,k-1);//然后进入左子树重复该操作,将两个序列带入函数,找到根节点,创建根节点,确认左右子树,直到子序列没数时(为空树的时候),返回NULL;
root->rchild=create(postL+leftnum,postR-1,k+1,inR);//递归式;
return root;
}
//广度优先搜索求层次序列;
void BFS(node* root){
queue<node* > Q;
//注意这里是指针队列,因为队列内存放的是二叉树结点的副本,如果想要对二叉树的数据进行修改,需要用到该结点的指针,去到该结点的位置进行修改,因此这存放的是指针;
Q.push(root);
int num=0;
while(!Q.empty()){
node* top=Q.front();
Q.pop();
printf("%d",top->data);
num++;
if(num<n)printf(" ");
if(top->lchild!=NULL)Q.push(top->lchild);
if(top->rchild!=NULL)Q.push(top->rchild);
}
}
int main( ){
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",&post[i]);
for(int i=0;i<n;i++)scanf("%d",&in[i]);
node* root=create(0,n-1,0,n-1);
BFS(root);
return 0;
}
#include<iostream>
#include<queue>
using namespace std;
struct node {
int data;
node* lchild;
node* rchild;
};
const int Maxn = 10010;
int post[Maxn];
int in[Maxn];
int n;
node* create(int postL, int postR, int inL, int inR) {
if(postL > postR)
return NULL;
node* root = new node;
root->data = post[postR];
int k;
for(k = inL; k <= inR; k++) {
if(in[k] == root->data)
break;
}
int Lnum = k - inL;
root->lchild = create(postL, postL + Lnum - 1, inL, k - 1);
root->rchild = create(postL + Lnum, postR - 1, k + 1, inR);
return root;
}
int num = 0;
void layerorder(node* root) {
queue<node* > q;
q.push(root);
while(!q.empty()) {
node* top = q.front();
q.pop();
cout << top->data;
if(++num < n) cout << " ";
if(top->lchild != NULL) q.push(top->lchild);
if(top->rchild != NULL) q.push(top->rchild);
}
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++)
cin >> post[i];
for(int i = 1; i <= n; i++)
cin >> in[i];
node* root = create(1, n, 1, n);
layerorder(root);
return 0;
}