From 96f8d117e26ca954c216f12b187c24f83aa6744b Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 31 Jul 2024 11:43:16 +0800 Subject: [PATCH] =?UTF-8?q?B3642=20=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/169648364 --- Luogu/B3642/B3642.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Luogu/B3642/B3642.cpp diff --git a/Luogu/B3642/B3642.cpp b/Luogu/B3642/B3642.cpp new file mode 100644 index 00000000..55d72667 --- /dev/null +++ b/Luogu/B3642/B3642.cpp @@ -0,0 +1,61 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e6 + 5; + +int n; +int lch[N], rch[N]; +int cnt_pre, pre[N], + cnt_in, in[N], + cnt_post, post[N]; + +void pre_order(int u) { + pre[++cnt_pre] = u; + + if (lch[u]) pre_order(lch[u]); + if (rch[u]) pre_order(rch[u]); +} + +void in_order(int u) { + if (lch[u]) in_order(lch[u]); + + in[++cnt_in] = u; + + if (rch[u]) in_order(rch[u]); +} + +void post_order(int u) { + if (lch[u]) post_order(lch[u]); + if (rch[u]) post_order(rch[u]); + + post[++cnt_post] = u; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> lch[i] >> rch[i]; + } + + pre_order(1); + in_order(1); + post_order(1); + + std::copy_n(pre + 1, n, std::ostream_iterator(cout, " ")); + cout << endl; + std::copy_n(in + 1, n, std::ostream_iterator(cout, " ")); + cout << endl; + std::copy_n(post + 1, n, std::ostream_iterator(cout, " ")); + cout << endl; + + return 0; +}