-
Notifications
You must be signed in to change notification settings - Fork 0
/
1020.cpp
81 lines (67 loc) · 1.52 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
72
73
74
75
76
77
78
79
80
//
// main.cpp
// test
//
// Created by 李寻欢 on 2019/9/25.
// Copyright © 2019 李寻欢. All rights reserved.
//
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
#include <cmath>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <unordered_map>
#include <string.h>
using namespace std;
const int MAXN = 50;
int n;
int in[MAXN];
int post[MAXN];
typedef struct node{
int data;
node* left;
node* right;
}Node,*TreeNode;
TreeNode build(int inL,int inR,int postL,int postR){
if(inL > inR) return NULL;
int k = 0;
for (int i=inL; i<=inR; i++) {
if(in[i] == post[postR]){
k = i;
break;
}
}
int numleft = k-inL;
TreeNode root = new Node;
root->data = post[postR];
root->left = build(inL, k-1, postL, postL+numleft-1);
root->right = build(k+1, inR, postL+numleft, postR-1);
return root;
}
void LevelOrder(TreeNode root){
queue<TreeNode> q;
q.push(root);
while (q.size()) {
int size = (int)q.size();
while (size--) {
n--;
TreeNode temp = q.front();
printf("%d",temp->data);
if(n!=0) printf(" ");
q.pop();
if(temp->left) q.push(temp->left);
if(temp->right) q.push(temp->right);
}
}
}
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]);
TreeNode root = build(0, n-1, 0, n-1);
LevelOrder(root);
}