SW Expert

[SWEA] 1231 중위순회 Python

꿀떡최고 2021. 5. 3. 10:28
반응형

[ 문제 ]

 

난이도:  D4

문제 번호:  1231

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV140YnqAIECFAYD&categoryId=AV140YnqAIECFAYD&categoryType=CODE&problemTitle=1231&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com


 

다음은 특정 단어(또는 문장)를 트리 형태로 구성한 것으로,

in-order 형식으로 순회하여 각 노드를 읽으면 원래 단어를 알 수 있다고 한다.

 

위 트리를 in-order 형식으로 순회할 경우 SOFTWARE 라는 단어를 읽을 수 있다.

 


 

[ 코드 ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def inorder(a):
    if a <= N:
        inorder(a*2)
        print(alp_li[a], end='')
        inorder(a*2+1)
 
for tc in range(111):
    N = int(input())
    alp_li = [0* (N+1)
    for i in range(N):
        li = list(input().split())
        alp_li[i+1= li[1]
    print(f'#{tc}', end=' ')
    inorder(1)
    print()
cs
반응형

'SW Expert' 카테고리의 다른 글

[SWEA] 1240 단순 2진 암호코드 Python  (0) 2021.05.05
[SWEA] 1232 사칙연산 Python  (0) 2021.05.04
[SWEA] 1208 Flatten Python  (0) 2021.05.02
[SWEA] 1204 최빈수 구하기Python  (0) 2021.05.01
[SWEA] 1206 View Python  (0) 2021.04.30