Approach

  Blog    |     January 29, 2026

To solve the problem of counting the hidden workers (leaf nodes) in a company hierarchy, we need to identify employees who do not have any subordinates. The hierarchy is represented by an array where each element indicates the manager of the corresponding employee. The CEO is identified by a manager value of -1.

  1. Problem Analysis: The task is to count all employees in the hierarchy who are not managers of any other employee. These employees are at the bottom of the hierarchy and are considered "hidden workers" as they do not manage anyone.
  2. Intuition: An employee is a leaf node (hidden worker) if no other employee has them as their manager. This can be determined by checking if the employee's index does not appear as a manager in the hierarchy array.
  3. Algorithm Selection:
    • Initialize a boolean array is_leaf of size n (number of employees) with all values set to True.
    • Iterate through each employee in the hierarchy. For each employee, if their manager is not -1 (i.e., they are not the CEO),

Request an On-site Audit / Inquiry

SSL Secured Inquiry