diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index 61e696bc..958d34fc 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -61,4 +61,5 @@ | 54차시 | 2024.05.14 | 다익스트라 | 미확인 도착지 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/185 | 55차시 | 2023.05.18 | 유니온 파인드 | 친구 네트워크 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/189 | 56차시 | 2023.05.18 | BFS | 공주님을 구해라! | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/193 +| 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