#C2403. Taco: Constructing an Odd-Valued Binary Tree
Taco: Constructing an Odd-Valued Binary Tree
Taco: Constructing an Odd-Valued Binary Tree
You are given an integer N
representing the number of nodes in a binary tree. Your task is to construct an odd-valued binary tree with N
nodes such that the sum of the depths of all nodes is minimized. The tree should be traversed in preorder (root, left, right) and the node values should be assigned consecutively in increasing order of odd numbers starting from 1.
The binary tree is constructed recursively. For a subtree with n
nodes (where n > 0
), assign the current odd number to the root, then allocate left = \(\left\lfloor{\frac{n}{2}}\right\rfloor\)
nodes to the left subtree and the remaining n - 1 - left
nodes to the right subtree. Continue this process until all nodes are placed.
For example, when N = 3
, one valid preorder traversal is [1, 3, 5]
(assigning 1 as the root, 3 in the left subtree, and 5 in the right subtree).
Note: Some variations in the structure may yield different valid preorder traversals if the tree is constructed in a different manner, but you should follow the method described above.
inputFormat
The input is read from stdin and has the following format:
T N1 N2 ... NT
Where T
(1 ≤ T ≤ 1000) is the number of test cases and each Ni
(1 ≤ Ni ≤ 104) is an integer representing the number of nodes for that test case.
outputFormat
For each test case, output the preorder traversal of the constructed odd-valued binary tree on a new line. The values should be space-separated.
## sample1
1
1
</p>