Find Path
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.
Input format
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.
Constraints:
1 <= T <= 10^2
1 <= N <= 25 * 10^2
1 <= NODE_DATA <= 10^5
1 <= N1, N2<= 10^5
Time Limit: 1 second
CodingNinjas
author
2y
Tip 1 : I used graph traversal with an array
:
CodingNinjas
author
2y
Brute Force
Consider function FINDPATH that takes ROOT node, node N1 and node N2 as parameter and:
- Define PATH1 And PATH2 as an empty ArrayList.
- Check and store path from ROOT to N1 in PATH1 and from RO...read more
CodingNinjas
author
2y
Lowest Common Ancestor
Consider function FINDPATH that takes ROOT node, node N1 and node N2 as parameters and:
- Define PATH1 And PATH2 as an empty ArrayList.
- Find LCANODE by calling function FINDLCA for ...read more
Add answer anonymously...
Top Morgan Stanley Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Morgan Stanley Software Developer
>
Morgan Stanley Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app