• Home
  • About
    • JOOS photo

      JOOS

      Joos's blog

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[백준] 2263번 : 트리의 순회 with python3

14 Nov 2018

Reading time ~1 minute

문제: https://www.acmicpc.net/problem/2263

import sys
sys.setrecursionlimit(10**6)
read = lambda : sys.stdin.readline().strip()

n = int(read())

inorder = list(map(int, read().split()))
postorder = list(map(int, read().split()))

position = {}
for i in range(n):
    position[inorder[i]] = i


def solve(in_start, in_end, post_start, post_end):
    if in_start > in_end or post_start > post_end: return
    root = postorder[post_end]
    print(root, end=' ')
    p = position[root]
    left = p - in_start
    solve(in_start, p-1, post_start, post_start+left-1)
    solve(p+1, in_end, post_start+left, post_end-1)

solve(0, n-1, 0, n-1)




algorithm백준python Share Tweet +1