Problem Solving/BOJ & Programmers

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

geum 2021. 7. 7. 23:17

๋‚˜๋™๋นˆ ๋‹˜์˜ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ํŠธ๋ฆฌ(Tree) ์ž๋ฃŒ๊ตฌ์กฐ 10๋ถ„ ํ•ต์‹ฌ ์š”์•ฝ ๊ณผ ์—ฌ๋Ÿฌ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด์„œ ํŠธ๋ฆฌ์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ–ˆ๋‹ค. ๊ฐœ๋…์„ ๋ดค๋‹ค๋ฉด ๋ฐฑ์ค€์—์„œ ๋ฐ”๋กœ ์ ์šฉํ•ด๋ณด๋Š” ๊ฒƒ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•œ ์˜ˆ์˜๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ๐Ÿ˜Ž  


 

๋ฌธ์ œ

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

 

ํด๋ž˜์Šค ๋ฐ ์ˆœํšŒ ๊ฒฐ๊ณผ ์ถœ๋ ฅ์„ ์œ„ํ•œ ํ•จ์ˆ˜ ๊ตฌํ˜„

ํŠธ๋ฆฌ ๊ตฌํ˜„์€ ์ฒ˜์Œ์ด๋ผ ๋‚˜๋™๋นˆ ๋‹˜์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

# ํŠธ๋ฆฌ ๊ตฌ์กฐ ํ‘œํ˜„์„ ์œ„ํ•œ ๋…ธ๋“œ ํด๋ž˜์Šค ์ƒ์„ฑ
class Node:
    def __init__(self, root, left_node, right_node):
        self.root = root		# ๋ถ€๋ชจ ๋…ธ๋“œ
        self.left_node = left_node	# ์™ผ์ชฝ ๋…ธ๋“œ
        self.right_node = right_node	# ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ
    
# ์ „์œ„ ์ˆœํšŒ
def preorder(node):
    print(node.root, 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.root, 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.root, end='')

 

๊ฐ ์ˆœํšŒ ๋ฐฉ์‹์˜ ํฐ ์ฐจ์ด์ ์€ ๋…ธ๋“œ ์ถœ๋ ฅ ์ˆœ์„œ์ด๋‹ค. ์ „์œ„/์ค‘์œ„/ํ›„์œ„ ์ˆœํšŒ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๋ฐ ํŠธ๋ฆฌ๋Š” ํฐ ํŠธ๋ฆฌ ์•ˆ์— ์„œ๋ธŒ ํŠธ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์žฌ๊ท€์˜ ๊ฐœ๋…๋„ ๋“ค์–ด๊ฐ„๋‹ค. ํด๋ž˜์Šค๋ฅผ ๋งŽ์ด ์จ๋ณด์ง€ ์•Š์•„์„œ ๋…ธ๋“œ ํด๋ž˜์Šค ๊ตฌํ˜„ ๋ถ€๋ถ„์ด ์ƒ์†Œํ–ˆ์ง€๋งŒ ํด๋ž˜์Šค์— ์ต์ˆ™ํ•œ ์‚ฌ๋žŒ์ด๋ผ๋ฉด ์ด ๋ฌธ์ œ๋ฅผ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ๊ฒƒ ๊ฐ™๋‹ค.

 

 

์ „์ฒด ์ฝ”๋“œ

# Silver 1

class Node:
    def __init__(self, root, left_node, right_node):
        self.root = root
        self.left_node = left_node
        self.right_node = right_node
    
def preorder(node):
    print(node.root, 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.root, 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.root, end='')

# ๋…ธ๋“œ ๊ฐœ์ˆ˜
n = int(input())

# ๋ถ€๋ชจ/์ž์‹ ๋…ธ๋“œ ๊ด€๊ณ„ ํ‘œํ˜„์„ ์œ„ํ•œ ๋”•์…”๋„ˆ๋ฆฌ
# ex) A(๋ถ€๋ชจ): [B, C](์ž์‹)
tree = {}

for i in range(n):
    root, left_node, right_node = input().split()
    
    tree[root] = Node(root, left_node, right_node)

preorder(tree['A'])
print()
inorder(tree['A'])
print()
postorder(tree['A'])

 

 

'Problem Solving > BOJ & Programmers' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ] 11256๋ฒˆ: Jelly Bean  (0) 2022.02.03
[BOJ] 5177๋ฒˆ: Format Error!  (0) 2022.02.03
[BOJ] 1806๋ฒˆ: Subsequence (๋ถ€๋ถ„ํ•ฉ)  (0) 2021.07.13
[BOJ] 2003๋ฒˆ: ์ˆ˜๋“ค์˜ ํ•ฉ 2  (0) 2021.07.09
[BOJ] 1260๋ฒˆ: DFS์™€ BFS  (2) 2021.06.27