반응형
내 풀이
n = int(input())
li = list(map(int, input().split()))
check = [0] * (2 ** n - 1)
ans = []
for _ in range(n):
temp = []
flag = True
for i, l in enumerate(li):
if flag and check[i] == 0:
check[i] = 1
temp.append(l)
flag = False
elif not flag and check[i] == 0:
flag = True
ans.append(temp)
for a in ans[::-1]:
print(*a)
다른 사람의 풀이
import sys
input = sys.stdin.readline
K = int(input())
_input = list(map(int, input().split()))
tree = [[] for _ in range(K)]
def makeTree(arr, x):
mid = (len(arr)//2)
tree[x].append(arr[mid])
if len(arr) == 1:
return
makeTree(arr[:mid], x+1)
makeTree(arr[mid+1:], x+1)
makeTree(_input, 0)
for i in range(K):
print(*tree[i])