
/**
* 深度遍历的一个简单例子
*
*/
public class EmployeeImportance {
private static int res = 0;
private static Employee employee;
public static void main(String[] args) {
Employee e1 = new Employee(1, 5, Arrays.asList(2,3));
Employee e2 = new Employee(2, 3, null);
Employee e3 = new Employee(3, 3, null);
int total = getImportance(Arrays.asList(e1,e2,e3), 1);
System.out.println(total);
}
public static int getImportance(List<Employee> emps, int id) {
for(Employee emp : emps) {
if(emp.id == id) {
employee = emp;
res += emp.value;
break;
}
}
List<Integer> list = employee.subordinates;
if(list != null) {
for(Integer i : list) {
getImportance(emps, i);
}
}
return res;
}
}
class Employee {
int id;
int value;
List<Integer> subordinates;
public Employee(int id, int value, List<Integer> subordinates) {
super();
this.id = id;
this.value = value;
this.subordinates = subordinates;
}
}