From ee606091f9d199d5780dc8a93e7d82b7db550aca Mon Sep 17 00:00:00 2001 From: tgyuu-An Date: Sun, 26 May 2024 00:53:10 +0900 Subject: [PATCH] 2024-05-26 --- tgyuuAn/README.md | 1 + ...4\354\235\230 \354\210\234\355\232\214.py" | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 "tgyuuAn/\353\266\204\355\225\240 \354\240\225\353\263\265/\355\212\270\353\246\254\354\235\230 \354\210\234\355\232\214.py" diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index f7cff13b..3b3b0fa1 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -60,4 +60,5 @@ | 53차시 | 2024.05.09 | 백트래킹 | 2048 (Easy) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/184 | 54차시 | 2024.05.14 | 다익스트라 | 미확인 도착지 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/185 | 55차시 | 2023.05.18 | 유니온 파인드 | 친구 네트워크 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/189 +| 57차시 | 2023.05.26 | 분할 정복 | 트리의 순회 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/197 --- diff --git "a/tgyuuAn/\353\266\204\355\225\240 \354\240\225\353\263\265/\355\212\270\353\246\254\354\235\230 \354\210\234\355\232\214.py" "b/tgyuuAn/\353\266\204\355\225\240 \354\240\225\353\263\265/\355\212\270\353\246\254\354\235\230 \354\210\234\355\232\214.py" new file mode 100644 index 00000000..a4db3430 --- /dev/null +++ "b/tgyuuAn/\353\266\204\355\225\240 \354\240\225\353\263\265/\355\212\270\353\246\254\354\235\230 \354\210\234\355\232\214.py" @@ -0,0 +1,27 @@ +import sys + +sys.setrecursionlimit(10**6) +def input(): return sys.stdin.readline().rstrip() + +N = int(input()) +in_order = list(map(int,input().split())) +post_order = list(map(int,input().split())) +position = [0 for _ in range(N+1)] + +for idx, element in enumerate(in_order): + position[element] = idx + +def divide_and_conquer(in_start, in_end, post_start, post_end): + if in_start > in_end or post_start > post_end: return + + in_order_root = post_order[post_end] + print(in_order_root, end=" ") + + root_idx = position[in_order_root] + + divide_and_conquer(in_start, root_idx-1, post_start, post_start+(root_idx - in_start) -1) + divide_and_conquer(root_idx+1, in_end, post_start+(root_idx - in_start), post_end-1) + + return + +divide_and_conquer(0, N-1, 0, N-1) \ No newline at end of file