Ctrl K

Evenly Split Teams

Medium
A company has n employees numbered 0 to n - 1, where n = len(links) + 1. Each pair [a, b] in links is a manager–report link, but it does not say which of the two is the boss. All you know is that employee 0 is at the top and the links form a tree. A *branch* of an employee is one direct report together with everyone below that report. An employee has an *evenly split team* if all of their branches have the same size. Anyone with no reports or a single report counts as evenly split. Return the number of employees with an evenly split team.
Example 1
links=[[0, 1], [0, 2], [1, 3], [2, 4]] → 5

Employee 0 has two branches of 2 people each, 1 and 2 have a single report each, and 3 and 4 have none. All five qualify.

Example 2
links=[[0, 1], [0, 2], [2, 3]] → 3

Employee 0's branches are {1} and {2, 3}, sized 1 and 2, which differ. The other three qualify, so the answer is 3.

Example 3
links=[[1, 0]] → 2
Constraints
  • 2 ≤ n ≤ 10⁵
  • len(links) = n − 1
  • 0 ≤ a, b ≤ n − 1
  • The links form a tree
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsIs It a Search Tree? MediumTwo Mixed-Up Nodes MediumAll Leaf Paths With a Given Total MediumTree Into a Right Chain MediumDigit Branch Totals Medium
loading Python…
Input
links=[[0, 1], [0, 2], [1, 3], [2, 4]]
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 49 tests on the server, hidden ones included.