No.566 だいたい完全二分木
問題文最終更新日: 2026-06-26 16:16:06
English (AI translation)
Almost Complete Binary Tree
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Show the original
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Show the original
yukicoder contest 173
(順位表)
の他の問題:
Problem Statement
An almost complete binary tree is a binary tree with $2^{K}-1$ vertices whose height is at least $K$ and at most $3K$. The height is the distance from the root to the farthest vertex, where each edge has length $1$.Today, once again, you have decided to construct $1$ almost complete binary tree.
The construction procedure is determined by a sequence $P_1, P_2, \ldots, P_{2^K - 1} $ that is a permutation of $\{1, 2, 3 , \ldots, 2^{K} - 1 \}$.
Specifically, starting from a binary search tree $T$ consisting of $0$ elements, you insert $P_1, P_2, \ldots , P_{2^{K}- 1}$ in this order.
The rule for inserting a value $v$ into a binary search tree $G$ is defined recursively as follows.
- If $G$ has no vertices, make $v$ the root of $G$.
- If $G$ has vertices, compare $v$ with the value of the root. If $v$ is smaller, insert it into the left subtree; otherwise, insert it into the right subtree.
The binary tree obtained from $( 1,3,2 )$ has height $2$, so it is an almost complete binary tree, but the binary tree obtained from $(2,1,3 )$ has height $1$, so it is not an almost complete binary tree.
Find any $1$ sequence such that the binary search tree $T$ obtained by this procedure is an almost complete binary tree.
It is guaranteed that at least $1$ solution exists.
Input
$K$
An integer $K(2 \leq K \leq 12)$ is given in $1$ line.
Output
Output the sequence $P$ that determines the construction procedure of a binary tree satisfying the condition, separated by spaces, in $1$ line.
Any sequence satisfying the condition will be accepted.
Sample
Sample 1
Input
2
Output
1 3 2
This is as shown in the problem statement.
The binary trees constructed from the following $4$ sequences, $ (1, 2, 3 ), (1,3,2 ), ( 3,1,2 ), ( 3,2,1 )$, have height $2$ and satisfy the condition.
The binary trees constructed from $ ( 2,3,1 ), ( 2,1,3 )$ have height $1$, so they do not satisfy the condition.
Sample 2
Input
3
Output
5 3 2 4 1 6 7
The following binary tree is obtained.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
紙ぺーぱー
ixmel