프로그래밍/백준

백준 9934 - 완전이진트리 (파이썬)

beans3142 2022. 3. 29. 14:44

난이도

실버 1

 

유형

트리 - 순회

 

해결

트리의 각 순회의 특징을 잘 이해했다면 쉽게 풀 수있다.

주어지는 이동 순서는 꽉 찬 이진트리에서 중위순회를 한 결과와 똑같다.

그것을 토대로 구현해주면 된다.

코드

from sys import stdin
input=stdin.readline

k=int(input())
order=list(map(int,input().split()))

line=[(len(order)//2,0,len(order))]
while True:
    nextline=[]
    for i,mn,mx in line:
        print(order[i],end=' ')
        nextline.append(((i+mn)//2,mn,i))
        nextline.append(((i+mx)//2,i,mx))
    print()
    line=nextline
    if len(line)==2**k:
        break

좋은 / 비슷한 문제