백준 풀이

백준 1991번 파이썬 (트리)

ag2개발자 2022. 9. 1. 17:07
n=int(input())
t={}
for i in range(n):
    a,b,c=map(str,input().split())
    t[a]=[b,c]    #left, right 자식
def preorder(root):
    if root!=".":
        print(root,end="")
        preorder(t[root][0])
        preorder(t[root][1])
def inorder(root):
    if root!=".":
        inorder(t[root][0])
        print(root,end="")   
        inorder(t[root][1])
def postorder(root):
    if root!=".":
        postorder(t[root][0])
        postorder(t[root][1])    
        print(root,end="")    

preorder('A')
print()
inorder('A')
print()
postorder('A')