
Ninja is chasing an enemy hiding in a labeled node 'LABEL' in an infinite binary tree where every node has two children. The nodes are labeled in row order but the enemy has shifted the order of the nodes of the tree to hide from the Ninja in such a way that now in the odd-numbered level (ie., the first, third, fifth,...), the labeling is left to right, while in the even-numbered level (second, fourth, sixth,...), the labeling is right to left.

Now given the ‘LABEL’ of the node in the tree where the enemy is hiding, return the labels of the nodes in the shortest path from the root of the tree to the node 'LABEL' which Ninja should follow to reach and defeat the enemy quickly.
Example:
Suppose given ‘LABEL’ is ‘14’ then:
The shortest path in the zigzag binary tree from the root where the Ninja starts to reach the node with the given label where the enemy is hiding is 1 -> 3 -> 4 -> 14.
The first line of input contains a single integer ‘T’ denoting the number of test cases given. Then next ‘T’ lines follow:
The first line of each test case input contains a single integer i.e. the ‘LABEL’ of the node where the enemy is hiding.
Output Format:
For every test case, print a single line containing space-separated integers denoting the labels of the nodes in the shortest path from the root of the tree to the node 'LABEL' which Ninja can follow to reach and defeat the enemy quickly.
Print the list/vector of the elements from the root to the node 'LABEL'.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10 ^ 4
1 <= ‘LABEL’ <= 10 ^ 6
Time Limit: 1 sec.
Where 'T' denotes the number of test cases and 'LABEL' denotes the label of the node the enemy is hiding in.
Time limit: 1 sec.
2
26
44
1 2 6 10 26
1 2 6 11 25 44
Test Case 1:
The output [1, 2, 6, 10, 26] is the required shortest list of labels which Ninja can travel to reach and defeat the enemy.
Test Case 2:
The output [1, 2, 6, 11, 25, 44] is the required shortest list of labels which Ninja can travel to reach and defeat the enemy.
2
14
50
1 3 4 14
1 3 5 12 22 50
Try to first build a tree that contains nodes for all the values up until the “label” which is given as input.
The basic idea behind the algorithm is fairly to realize that if you take the node that corresponds to the label given as input then you can assign an index value to that node. Then take that index value and divide it by two. This will give the index value of the parent node and keep repeating the above idea to get all the nodes from root to label.
The algorithm to calculate the required will be:-
O(N + log(N)) ~ O(N), where ‘N’ is the value of the given ‘LABEL’.
Since we are building a tree with indexes level for the zig-zag tree by iterating over all the nodes which takes O(N) time. After that for every node, we are also finding the parent node until we reach the root node (which is 1) which is reducing the traversal of the tree in the order of the depth i.e. log(N). Hence the complexity here grows by O(N + log(N)) ~ O(N).
O(N + log(N)) ~ O(N), where ‘N’ is the value of the given ‘LABEL’
Since, here, we require a log(N) amount of space, i.e equal to the depth of the tree which we are traversing. But we are also building up level-wise indexes of the nodes in the binary tree which requires O(N) space, therefore, the space complexity is O(N + log(N)) ~ O(N).