PAT甲級 1020
題目 Tree Traversals
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.
解析
已知二叉樹的後序遍歷、中序遍歷,求二叉樹的層序遍歷
代碼
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *lchild,*rchild;
};
int n,ind=0;
int postorder[35], inorder[35],preorder[35];
node * res;
node* create(int i1,int i2, int p1, int p2){
if(i1>i2 || p1>p2) return NULL;
node *root = new node;
root->data = postorder[p2];
int k;
for(k = i1; k < i2;k++)
if(root->data == inorder[k])
break;
root->lchild = create(i1, k-1, p1, p1+k-i1-1);
root->rchild = create(k+1, i2, p2+k-i2, p2-1);
return root;
}
void bfs(node *root){
queue<node*> q;
q.push(root);
while(!q.empty()){
node *root = q.front();
q.pop();
preorder[ind++] = root->data;
if(root->lchild !=NULL)
q.push(root->lchild);
if(root->rchild !=NULL)
q.push(root->rchild);
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++)
cin>>postorder[i];
for(int i=0;i<n;i++)
cin>>inorder[i];
res = create(0, n-1, 0, n-1);
bfs(res);
for(int i=0;i<n;i++){
cout<<preorder[i];
if(i!=n-1){
printf(" ");
}
}
return 0;
}