반응형
https://www.acmicpc.net/problem/1991
class Node:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def bTree(node):
if node.val == '.':
return None
node.left = bTree(Node(link[node.val][0]))
node.right = bTree(Node(link[node.val][1]))
return node
n = int(input())
link = {}
for _ in range(n):
node, left, right = map(str, input().split())
link[node] = (left, right)
head = Node('A')
mTree = bTree(head)
global preord
global inord
global postord
preord = ""
inord = ""
postord = ""
def preorder(node):
global preord
if node:
preord += node.val
preorder(node.left)
preorder(node.right)
def inorder(node):
global inord
if node:
inorder(node.left)
inord += node.val
inorder(node.right)
def postorder(node):
global postord
if node:
postorder(node.left)
postorder(node.right)
postord += node.val
preorder(head)
inorder(head)
postorder(head)
print(preord)
print(inord)
print(postord)
기본적이면서도 중요한 문제다!
댓글