We are given a tree of N nodes from 1 to N where node 1 is the root of the tree. For each node i from 1 to N, you have to find the numbers of nodes which are in the subtree of i and are at distance equal to height of subtree of i.
Distance between a & b = total number of edges in path from a to b,height of subtree = maximum distance of the root of the subtree to any node in the subtree.
I have tried to create an algorithm using dfs using adjancey list of arraylist in java but got nowhere.
example input and output: input:
first line: 4--> no of nodes--- next each line represents connection of nodes (1 2), (2 3), (2 4)
-->space separated output for each node as a root. 2 2 1 1