From bcdf5b6455e70d4928cf767ee319ab991720d12e Mon Sep 17 00:00:00 2001 From: jungwoo <127028334+jungwoo1208@users.noreply.github.com> Date: Sat, 22 Jun 2024 12:48:07 +0900 Subject: [PATCH] =?UTF-8?q?=ED=8A=B8=EB=A6=AC=EC=9D=98=20=EC=88=9C?= =?UTF-8?q?=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Boj2263.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Boj2263.cpp diff --git a/Boj2263.cpp b/Boj2263.cpp new file mode 100644 index 00000000..9eb0ce01 --- /dev/null +++ b/Boj2263.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +void tree(const vector& in, const vector& post, int in_start, int in_end, int post_start, int post_end) { + if (post_start > post_end) { + return; + } + int root = post[post_end]; + cout << root << " "; + if (in_start == in_end) { + return; + } + int idx1 = 0; + for (int i = in_start; i <= in_end; i++) { + if (in[i] == root) { + idx1 = i; + break; + } + } + int left_size = idx1 - in_start; + + tree(in, post, in_start, idx1 - 1, post_start, post_start + left_size - 1); + tree(in, post, idx1 + 1, in_end, post_start + left_size, post_end - 1); +} + +int main() { + int n; + cin >> n; + vector in(n), post(n); + for (int i = 0; i < n; i++) { + cin >> in[i]; + } + for (int i = 0; i < n; i++) { + cin >> post[i]; + } + tree(in, post, 0, n - 1, 0, n - 1); + return 0; +}