标签:include using name 队列 color 链接 c++ 输出 div
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456
题解:根据后序中序建树,层序遍历输出的裸题,建树的基本思想是利用递归,层序遍历可以利用队列,详细参见代码
代码:
1 #include<bits/stdc++.h> 2 #define inf 0x3f3f3f3f 3 using namespace std; 4 typedef long long ll; 5 const int M = int(1e5) * 2 + 5; 6 int in[M], po[M]; 7 int ptr; 8 struct node { 9 int l, r, v; 10 }t[M]; 11 void build(int l, int r, int n) 12 { 13 if (l >= r) 14 { 15 t[n].v = inf; 16 return; 17 } 18 int root = po[ptr--]; 19 t[n].v = root; 20 t[n].l = n * 2; 21 t[n].r = n * 2 + 1; 22 int mid = find(in, in + r, root) - in; 23 build(mid + 1, r, n * 2 + 1); 24 build(l, mid, n * 2); 25 } 26 void Print() 27 { 28 queue<int> q; 29 q.push(1); 30 int id; 31 while (!q.empty()) 32 { 33 id = q.front(); 34 q.pop(); 35 if (t[id].v != inf) 36 { 37 if (id != 1) 38 cout << " "; 39 cout << t[id].v; 40 q.push(t[id].l); 41 q.push(t[id].r); 42 } 43 } 44 } 45 int main() 46 { 47 int n; cin >> n; 48 for (int i = 0; i < n; i++) cin >> po[i]; 49 for (int i = 0; i < n; i++) cin >> in[i]; 50 ptr = n - 1; 51 build(0, n, 1); 52 Print(); 53 return 0; 54 }
标签:include using name 队列 color 链接 c++ 输出 div
原文地址:https://www.cnblogs.com/harutomimori/p/10808283.html