1138 Postorder Traversal (25 point(s))
发布日期:2021-05-18 12:17:45 浏览次数:19 分类:精选文章

本文共 613 字,大约阅读时间需要 2 分钟。

#include

#include
#include
using namespace std;

const int N = 5e4 + 10;int n, in[N], pre[N];unordered_map<int, int> pos;int l, r;bool s;

int build(int il, int ir, int pl, int pr) {int root = pre[pl];int k = pos[root];if (k > il) {l[root] = build(il, k-1, pl+1, pl+k-il);}if (k < ir) {r[root] = build(k+1, ir, pl+k-il+1, pr);}return root;}

void post(int u) {if (l.count(u)) {post(l[u]);}if (r.count(u)) {post(r[u]);}if (!s) {s = "1";cout << u;}}

int main() {cin >> n;for (int i = 0; i < n; i++) {cin >> pre[i];}for (int i = 0; i < n; i++) {cin >> in[i];pos[in[i]] = i;}int root = build(0, n-1, 0, n-1);post(root);}

上一篇:1094 The Largest Generation (25 point(s))
下一篇:1110 Complete Binary Tree (25 point(s))

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年04月29日 17时43分51秒