Find all employees who directly or indirectly reports to a manager

Given a map containing employee to manager mappings, find all employees under each manager who directly or indirectly reports to him.


For example, consider below employee-manager pairs:

A -> A
B -> A
C -> B
D -> B
E -> D
F -> E

Here, A reports to himself i.e. A is head of the company and is the manager of employee B. B is the manager of employees C and D, D is the manager of employee E, E is the manager of employee F, C and F are not managers of any employee.


A -> B, D, C, E, F
B -> D, C, E, F
C ->
D -> E, F
E -> F
F ->

The idea is to construct a reverse map, containing manager to employee mappings, and recursively find all reporting employees (direct and indirect) in the hierarchy for every manager. The algorithm can be implemented as follows in C++ –


Download   Run Code


A -> B D C E F
B -> D C E F
C ->
D -> E F
E -> F
F ->

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)


Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

newest oldest most voted
Notify of

is this solution ok?