-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1020.cpp
71 lines (66 loc) · 1.32 KB
/
1020.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// 1020.cpp
// 算法
//
// Created by 王怡凡 on 2017/8/20.
// Copyright © 2017年 王怡凡. All rights reserved.
//
#include <stdio.h>
#include<queue>
using namespace std;
const int maxn = 40;
int n,post[maxn],in[maxn];
struct node {
int v;
node *lchild, *rchild;
};
void bfs(node* root) {
queue<node*> q;
q.push(root);
int num=0;
while(!q.empty()) {
node* now = q.front();
printf("%d",now->v);
num++;
if(num!=n) {
printf(" ");
}
q.pop();
if(now->lchild!=NULL) {
q.push(now->lchild);
}
if(now->rchild!=NULL) {
q.push(now->rchild);
}
}
}
node* create(int postL,int postR,int inL,int inR) {
if(postL>postR) {
return NULL;
}
node* root = new node;
root->v = post[postR];
int i,k;
for(i=inL;i<=inR;i++) {
if(in[i]==post[postR]) {
k = i;
}
}
int numLeft = k - inL;
root->lchild = create(postL,postL+numLeft-1,inL,k-1);
root->rchild = create(postL+numLeft,postR-1,k+1,inR);
return root;
}
int main() {
int i;
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%d",&post[i]);
}
for(i=0;i<n;i++) {
scanf("%d",&in[i]);
}
node* root = create(0,n-1,0,n-1);
bfs(root);
return 0;
}