-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path1020_Tree Traversals (25).cpp
96 lines (87 loc) · 2.1 KB
/
1020_Tree Traversals (25).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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
using namespace std;
#define N 32
int postOrder[N];
int inOrder[N];
struct node
{
int val;
node *left;
node *right;
node(const int &v);
};
node::node(const int &v)
{
val = v;
left = right = NULL;
}
void build(node * &r, int post[],int in[],int postLeft,int postRight, int inLeft, int inRight)
{//构造以r为根的二叉树
if(inLeft > inRight)
{//二叉树无结点,空二叉树
r = NULL;
}
else
{
r = new node(post[postRight]);
int mid = inLeft;
while(in[mid] != post[postRight])
{//找出中序遍历中根的位置
mid++;
}
//cout << "根:" << post[postRight] << endl;
//cout << "左:" << postLeft << " " << postLeft+(mid-1) << " " << inLeft << " " << mid-1 << endl;
//cout << "右:" << postLeft+mid << " " << postLeft+mid-inLeft << " " << mid+1 << " " << inRight << endl;
//system("pause");
build(r->left, post, in, postLeft, postLeft+(mid-1-inLeft), inLeft, mid-1);
build(r->right, post, in, postLeft+mid-inLeft, postRight-1, mid+1, inRight);
}
}
int main()
{
int n,i;
while(cin >> n)
{
for(i = 0; i < n; i++)
{
cin >> postOrder[i];
}
for(i = 0; i < n; i++)
{
cin >> inOrder[i];
}
node *root;
build(root, postOrder, inOrder, 0, n-1, 0, n-1);
vector<int> ans;
queue<node*> q;
if(root != NULL)
{
q.push(root);
}
while(!q.empty())
{
node *cur = q.front();
ans.push_back(cur->val);
q.pop();
if(cur->left != NULL)
{
q.push(cur->left);
}
if(cur->right != NULL)
{
q.push(cur->right);
}
}
for(i = 0; i < ans.size()-1; i++)
{
cout << ans[i] << " ";
}
cout << ans[i] << endl;
}
return 0;
}