I have this class object called Person
public class Person {
....
private List<Person> people;
....
public List<Person> getPeople() {
return people;
}
public void setPeople(List<Person> people) {
this.people = people;
}
Each person have list of all employees inside and that each person has List of people underneath. How to find the max deep? for example, in that image, max deep is 2. second highest is 1. Any help is appreciated.
Simply:
public static int maxDepth(Person p) {
int maxChildrenDepth = 0;
for (Person c: p.getPeople()) {
maxChildrenDepth = Math.max(maxChildrenDepth, maxDepth(c));
}
return 1 + maxChildrenDepth;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With