๊ฐœ๋ฐœ/๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

baekjoon. ํŠธ๋ฆฌ ์ˆœํšŒ [Silver I] [python]

ttoance 2023. 3. 25. 23:34

๋ฌธ์ œ๋งํฌ : 

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

 

1991๋ฒˆ: ํŠธ๋ฆฌ ์ˆœํšŒ

์ฒซ์งธ ์ค„์—๋Š” ์ด์ง„ ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 26)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ ๋…ธ๋“œ์™€ ๊ทธ์˜ ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋…ธ๋“œ์˜ ์ด๋ฆ„์€ A๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์•ŒํŒŒ

www.acmicpc.net

class Node:
    def __init__(node, data, left_node, right_node):
        node.data = data
        node.left_node = left_node
        node.right_node = right_node
        

n = int(input())
tree = {}

def preOrder(node):
    print(node.data, end = '')
    if (node.left_node != '.'):
        preOrder(tree[node.left_node])
    if (node.right_node != '.'):
        preOrder(tree[node.right_node])
        
def inOrder(node):
    if (node.left_node != '.'):
        inOrder(tree[node.left_node])
    print(node.data, end = '')
    if (node.right_node != '.'):
        inOrder(tree[node.right_node])

def postOrder(node):
    if (node.left_node != '.'):
        postOrder(tree[node.left_node])
    if (node.right_node != '.'):
        postOrder(tree[node.right_node])
    print(node.data, end = '')

# tree set 
for _ in range(n):
    data, left_node, right_node = input().split(' ')
    tree[data] = Node(data, left_node, right_node)
    
preOrder(tree['A'])
print()
inOrder(tree['A'])
print()
postOrder(tree['A'])
print()
๋ฐ˜์‘ํ˜•