Skip to content

Latest commit

 

History

History
50 lines (34 loc) · 876 Bytes

boj_1991.md

File metadata and controls

50 lines (34 loc) · 876 Bytes

BOJ 1991. 트리 순회

문제 링크

[BOJ 1991] 트리 순회

분류

트리, 재귀

코드

import sys

N = int(sys.stdin.readline().strip())
tree = {}

for n in range(N):
    root, left, right = sys.stdin.readline().strip().split()
    tree[root] = [left, right]


def preorder(root):
    if root != '.':
        print(root, end='')  # root
        preorder(tree[root][0])  # left
        preorder(tree[root][1])  # right


def inorder(root):
    if root != '.':
        inorder(tree[root][0])  # left
        print(root, end='')  # root
        inorder(tree[root][1])  # right


def postorder(root):
    if root != '.':
        postorder(tree[root][0])  # left
        postorder(tree[root][1])  # right
        print(root, end='')  # root


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