N = int(input())
inputlist = []
for i in range(1):
inputlist.append(input())
numlist = inputlist[0].split()
for i in range(N):
numlist[i] = int(numlist[i])
def fun(n, numlist):
if n == 1:
return [1]
height = 0
while 2 ** height - 1 < n:
height += 1
leefNode = numlist[2 ** (height-1) - 1:]
x = (2 ** (height - 1) - len(leefNode)) // 2 - 1
tmp = numlist[2 ** (height - 1) - 2 - x: 2 ** (height - 1) - 2]
leftNode = []
rightNode = []
for i in range(height - 1):
if i == 0:
leftNode.append(2 ** i)
else:
leftNode.append(2 ** i)
rightNode.append(2 ** (height - i) - 1)
return leftNode + leefNode + tmp + rightNode
res = fun(N, numlist)
for i in range(len(res)):
print(res[i], end=' ')
猿辅导编程第一题:完全二叉树 输入层序遍历序列,有没有大佬帮我看看为啥ac=0
全部评论
(0) 回帖