• Home
  • About
    • JOOS photo

      JOOS

      Joos's blog

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

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

11 Sep 2018

Reading time ~1 minute

https://www.acmicpc.net/problem/1991

left={}
right={}
for _ in range(int(input())):
    c,l,r=input().split()
    left[c]=l
    right[c]=r

def preorder(x):
    if x is '.':
        return ""
    else:
        return x+preorder(left[x])+preorder(right[x])

def inorder(x):
    if x is '.':
        return ""
    else:
        return inorder(left[x])+x+inorder(right[x])

def postorder(x):
    if x is '.':
        return ""
    else:
        return postorder(left[x])+postorder(right[x])+x

print(preorder('A'))
# 루트노드 시작이 A여서 A부터
print(inorder('A'))
print(postorder('A'))

간단한 문제여서 그냥 dictionary로 tree를 만들었다.



algorithm백준 Share Tweet +1