CR07C4P5 - JOGURT
A complete binary tree is made of nodes arranged in a hierarchic structure. One of the nodes is the root node, said to be at level 0. The root node has two child nodes, which are at level 1. Each of those has two children at level 2 etc.
In general, a complete binary tree with N levels has 2N–1 nodes, each of which has two child nodes, except those at level L–1.
A number can be written into each node. Write the numbers 1 to 2N–1 into a complete binary tree with N levels so that, for each node at level D, the absolute value of the difference of the sum of all numbers in the left subtree and the sum of all numbers in the right subtree is 2D.
For example, the sum of the left subtree of the root node must differ from the sum of the right subtree by 1. The sums of the left and right subtrees of a node at level 1 must differ by 2.
Each number must be used exactly once. The solution need not be unique.
Input
The first and only line of input contains the integer N (1 ≤ N ≤ 15), the number of levels in the tree.
Output
Output the 2N–1 separated by spaces on a single line, the binary tree in the preorder traversal. The preorder traversal first outputs the number in the root node, then outputs the left subtree (again in the< preorder traversal), then the right subtree.
Example
Input: 2 Output: 3 1 2
Input: 3 Output: 3 1 7 5 6 2 4
Added by: | Ahmed Salem [mrtempo] |
Date: | 2015-01-20 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 JS-MONKEY |
Resource: | COCI 2006/2007 #4 (http://hsin.hr/coci/archive/2006_2007/contest4_tasks.pdf) |