Height of every node in a tree

Given a tree of A nodes. Find the height of tree for each node taken as a root.

Input Format:

First argument of input contains an integer A denoting number of Nodes
Second argument of input contains a (A-1) x 2 matrix denoting edges of tree.

Output Format:

Return an integer array of size A denoting height of each node as a root.

Constraints:

1 ≤ N ≤ 100000

Example Input:

Input 1:
    A = 4 , B = [[1,2],[2,3],[2,4]]
Output 1:
    [2,1,2,2]
Explanation 1:
    For node 1 deepest node is 3(or 4) at height 2, for node 3,4 node 1 is deepest node at height 2 and for node 2 all node are at height 1.
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Height of every node in a tree on Interview Code Editor
Sign Up
to access hints and editorial solutions for Height of every node in a tree

Discussion


Loading...
Click here to start solving coding interview questions