๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1991_ํŠธ๋ฆฌ ์ˆœํšŒ

๋ฟŒ์•ผ._. 2022. 3. 7. 14:31

Silver I

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/1991)

<ํŠธ๋ฆฌ ์ˆœํšŒ>

๋ฌธ์ œ 

 

์ด์ง„ํŠธ๋ฆฌ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ „์œ„ ์ˆœํšŒ(preorder traversal), ์ค‘์œ„ ์ˆœํšŒ(inorder traversal), ํ›„์œ„ ์ˆœํšŒ(postorder traversal) ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์™€ ๊ฐ™์€ ์ด์ง„ํŠธ๋ฆฌ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด,

์ „์œ„ ์ˆœํšŒํ•œ ๊ฒฐ๊ณผ : ABDCEFG // (๋ฃจํŠธ) (์™ผ์ชฝ ์ž์‹) (์˜ค๋ฅธ์ชฝ ์ž์‹)
์ค‘์œ„ ์ˆœํšŒํ•œ ๊ฒฐ๊ณผ : DBAECFG // (์™ผ์ชฝ ์ž์‹) (๋ฃจํŠธ) (์˜ค๋ฅธ์ชฝ ์ž์‹)
ํ›„์œ„ ์ˆœํšŒํ•œ ๊ฒฐ๊ณผ : DBEGFCA // (์™ผ์ชฝ ์ž์‹) (์˜ค๋ฅธ์ชฝ ์ž์‹) (๋ฃจํŠธ)

๊ฐ€ ๋œ๋‹ค.

 

 

์ž…๋ ฅ

 

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

 

 

์ถœ๋ ฅ 

 

์ฒซ์งธ ์ค„์— ์ „์œ„ ์ˆœํšŒ, ๋‘˜์งธ ์ค„์— ์ค‘์œ„ ์ˆœํšŒ, ์…‹์งธ ์ค„์— ํ›„์œ„ ์ˆœํšŒํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฐ ์ค„์— N๊ฐœ์˜ ์•ŒํŒŒ๋ฒณ์„ ๊ณต๋ฐฑ ์—†์ด ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด

- my solution

import sys

def preorder(n,tree,answer): #์ „์œ„ ์ˆœํšŒ
    if n!='.':
        answer.append(n)
        if tree[n][0]!='.':
            preorder(tree[n][0],tree,answer)
        if tree[n][1]!='.':
            preorder(tree[n][1],tree,answer)

def inorder(n,tree,answer): #์ค‘์œ„ ์ˆœํšŒ
    if n!='.':
        if tree[n][0]!='.':
            inorder(tree[n][0],tree,answer)
        answer.append(n)
        if tree[n][1]!='.':
            inorder(tree[n][1],tree,answer)

def postorder(n,tree,answer): #ํ›„์œ„ ์ˆœํšŒ
    if n!='.':
        if tree[n][0]!='.':
            postorder(tree[n][0],tree,answer)
        if tree[n][1] != '.':
            postorder(tree[n][1], tree, answer)
        answer.append(n)

if __name__=='__main__':
    n=int(input())
    tree={}

    for i in range(n):
        temp=sys.stdin.readline().split()
        tree[temp[0]]=[temp[1],temp[2]]

    answer=[]
    #์ „์œ„ ์ˆœํšŒ
    preorder('A', tree, answer)
    print(''.join(answer))

    answer=[]
    #์ค‘์œ„ ์ˆœํšŒ
    inorder('A',tree,answer)
    print(''.join(answer))

    answer=[]
    #ํ›„์œ„ ์ˆœํšŒ
    postorder('A',tree,answer)
    print(''.join(answer))

 

  • preorder(๋…ธ๋“œ, tree dict, answer)
    : ์ „์œ„ ์ˆœํšŒ - ๋ฃจํŠธ -> ์™ผ์ชฝ ์ž์‹ -> ์˜ค๋ฅธ์ชฝ ์ž์‹
  • inorder(๋…ธ๋“œ, tree dict, answer)
    : ์ค‘์œ„ ์ˆœํšŒ - ์™ผ์ชฝ ์ž์‹ -> ๋ฃจํŠธ -> ์˜ค๋ฅธ์ชฝ ์ž์‹
  • postorder(๋…ธ๋“œ, tree dict, answer)
    : ํ›„์œ„ ์ˆœํšŒ - ์™ผ์ชฝ ์ž์‹ -> ์˜ค๋ฅธ์ชฝ ์ž์‹ -> ๋ฃจํŠธ
  • tree dict
    : key= ๋…ธ๋“œ
    : value= [์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ]

 


์ƒ๊ฐ๐Ÿค”

 

์ฑ…์—์„œ ํŠธ๋ฆฌ ๋ถ€๋ถ„์„ ๋ณด๊ณ  ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. ์ฑ…์—์„œ๋Š” class๋กœ ๊ตฌํ˜„ํ•˜์—ฌ left์™€ right๋ฅผ ํ™•์ธํ•˜์˜€์ง€๋งŒ,

์ด ๋ฌธ์ œ์—์„œ dict๋กœ๋„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„ dict๋กœ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

 

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ ํ›„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด๋ฉฐ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€๋Š”์ง€ ํ™•์ธํ•ด๋ณด๋‹ˆ,

class, dict, list์™€ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.