Question: Please write code in JAVA, thanks! Assume you are given a tree of N nodes. The nodes are numbered from 0 to N-1 and their

 Please write code in JAVA, thanks! Assume you are given a

Please write code in JAVA, thanks!

Assume you are given a tree of N nodes. The nodes are numbered from 0 to N-1 and their parents are represented in an array A, such that Alil denotes the number of the parent of the i-th node. Node 0 is the root node and it will not have a parent, so the corresponding value in array A will be-1. We define the distance between two nodes to be the length of the shortest path between them, and we define an ancestor of the i-th node as any node lying on the shortest path between the i-th node and the root. Your goal is to find the ancestor at distance D of every node of the tree Write a function clas8 Solution public intl) 8olution(int D, int Ah that, given an integer D and array A of N integers, returns an array of N integers representing the ancestors at distance D of the consecutive nodes. If a node doesn't have an ancestor at distance D its field should contain -1 For example, given integer D 2 and array A such that A10 Al32 your function should return [-1. -1,0,1,2]. Given D 3 and array A such that: A0-1 A[214 A[4]-1 your function should retum [-1,-1, 0, 1,-1]. Write an efficient algorithm for the following assumptions: . N and D are integers within the range [1..1,000]: the first element of array A equals -1 each element of array A, besides the first one, is an integer within the range [0.N-1] . array A represents a valid tree Assume you are given a tree of N nodes. The nodes are numbered from 0 to N-1 and their parents are represented in an array A, such that Alil denotes the number of the parent of the i-th node. Node 0 is the root node and it will not have a parent, so the corresponding value in array A will be-1. We define the distance between two nodes to be the length of the shortest path between them, and we define an ancestor of the i-th node as any node lying on the shortest path between the i-th node and the root. Your goal is to find the ancestor at distance D of every node of the tree Write a function clas8 Solution public intl) 8olution(int D, int Ah that, given an integer D and array A of N integers, returns an array of N integers representing the ancestors at distance D of the consecutive nodes. If a node doesn't have an ancestor at distance D its field should contain -1 For example, given integer D 2 and array A such that A10 Al32 your function should return [-1. -1,0,1,2]. Given D 3 and array A such that: A0-1 A[214 A[4]-1 your function should retum [-1,-1, 0, 1,-1]. Write an efficient algorithm for the following assumptions: . N and D are integers within the range [1..1,000]: the first element of array A equals -1 each element of array A, besides the first one, is an integer within the range [0.N-1] . array A represents a valid tree

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!