You are given a tree with 'N' nodes with 'N' - 1 distinct edge. You are also given two nodes 'N1' and 'N2'. You have to find and print the shortest path between 'N1' and 'N2'.
A tree data structure is a nonlinear hierarchical data structure that consists of nodes connected by edges.
Note
1. There is no cycle present in a tree.
2. All values in the tree are unique.
3. Both nodes will always exist in the tree.
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.
The first line of each test case contains elements of a tree in level order form separated by space.
Order is:
ROOT_DATA, 'N' (Number of children of the root), 'N' children, and so on for every element
The second line of each test case contains two space-separated integers 'N1' and 'N2'.
For example, the input for the tree depicted in the below image would be :

Explanation :
Level 1 :
The root node of the tree is 20
Level 2 :
1st child of 20 = 10
2nd child of 20 = 35
Level 3 :
1st child of 10 = 5
2nd child of 10 = 15
1st child of 35 = 30
2nd child of 35 = 42
Level 4 :
Children of 5 = 0
Ist child of 15 = 13
Children of 30 = 0
Children of 42 = 0
Level 5 :
Children of 13 = 0
The input ends when all nodes at the last level have 0 children.
Note:
A tree node may have zero or more child nodes.
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
20 2 10 35 2 5 15 2 30 42 0 1 13 0 0 0
Output Format
For each test case, print the shortest path from 'N1' to 'N2' in a single line.
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^2
1 <= N <= 25 * 10^2
1 <= NODE_DATA <= 10^5
1 <= N1, N2<= 10^5
Time Limit: 1 second
3
1 0
1 1
1 2 2 3 0 1 4 0
1 2
1 1 2 1 3 1 4 0
1 3
1
1 2
1 2 3
(i) Since there is only one node from node- 1 to node 1 so, we print only 1 as the path.
(ii) Since 1 and 2 are directly connected so, we print 1 2 as the path.
(iii) Going from 1 to 3 we have 2 as the child of 1 and 3 as the child of 2. Hence the path is 1 2 3.
3
1 2 2 3 2 4 5 2 6 7 0 0 0 0
1 5
1 1 2 1 3 0
2 3
1 1 2 2 3 4 2 5 6 2 7 8 0 0 0 0
1 8
1 2 5
2 3
1 2 4 8
(i) Going from 1 to 5 we have 2 as the child of 1 and 4 and 5 as the child of 2. Hence the path is 1 2 5.

(ii) Since 2 and 3 are directly connected so, we print 2 3 as the path.
(iii) Going from 1 to 8 we have 2 as the child of 1, 3 and 4 as the child of 2 and 7 and 8 as the child of 4. Hence the path is 1 2 4 8.

Try to think of how you can find a node that is ancestor to both nodes and use this node to get path.
Consider function FINDPATH that takes ROOT node, node N1 and node N2 as parameter and:
We define function HASPATH that accepts tree node ROOT, array list of integers PATH and integer X as parameters and:
We define function PRINTPATH that accepts array list of integers PATH1 and PATH2 as parameters and:
O(N) where N is the number of nodes in the tree.
We are finding the path between two nodes which will take linear time because in the worst case we have to visit all nodes of the tree to check if it is in the path or not. Hence the time complexity of the algorithm will be O(N).
O(N) where N is the number of nodes in the tree.
The space complexity of the algorithm is O(N) because we are using extra arrays to store the path and also because of recursion stack space in memory.