This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
n = int(input()) | |
graph = {} | |
for i in range(n): | |
x, y, z = sys.stdin.readline().split() | |
graph[x] = (y, z) | |
def preorder(root): | |
print(root, end='') | |
for a in graph[root]: | |
if a != '.': | |
preorder(a) | |
def inorder(root): | |
if graph[root][0] != '.': | |
inorder(graph[root][0]) | |
print(root, end='') | |
if graph[root][1] != '.': | |
inorder(graph[root][1]) | |
def postorder(root): | |
for a in graph[root]: | |
if a != '.': | |
postorder(a) | |
print(root, end='') | |
preorder('A') | |
print() | |
inorder('A') | |
print() | |
postorder('A') |
전위 순회는 뿌리->왼쪽 자식->오른쪽 자식 순
중위 순회는 왼쪽자식-> 뿌리-> 오른쪽 자식
후위 순회는 왼쪽자식->오른쪽 자식-> 뿌리
'코딩테스트 준비' 카테고리의 다른 글
재귀, 분할정복 - 백준 쿼드트리 (0) | 2021.02.25 |
---|---|
LIS, DP - 백준 - 가장 긴 바이토닉 부분 수열 (0) | 2021.02.24 |
재귀 + dp + 조합 공식 (0) | 2021.02.24 |
서로소 집합 알고리즘 (0) | 2021.02.24 |
itertools의 combinations를 이용한 부분수열 합 문제 (0) | 2021.02.24 |