Team size: hints, no solution
Easy · Trees
A company records who reports to whom as a binary reporting tree: every employee has at most two direct reports, a left one and a right one. Each employee has a unique positive id, and the tree holds those ids.
The tree is given in level order: the root first, then the children of each present node from left to right, with null standing for a missing child. Nodes below a missing child are not listed.
Given the tree and the id of a manager, return the size of that manager's team: the manager plus everyone who reports to them directly or through other people. If no employee has that id, return 0. An empty tree is the empty list.
Examples
- Input
- tree = [10, 4, 7, 2, 5, null, 9], manager = 4
- Output
- 3
- Note
- Employee 4 has direct reports 2 and 5, so the team is 4, 2 and 5.
- Input
- tree = [10, 4, 7, 2, 5, null, 9], manager = 7
- Output
- 2
- Note
- Employee 7 has a single direct report, 9, on the right.
Constraints
- 0 <= number of employees <= 2000
- Ids are unique integers from 1 to 100000
- manager is an integer from 1 to 100000
Hint 1: a nudge
If you pointed at one employee, which other employees would be counted with them? Try it on a small tree and on the top of the tree.
Unlock the full hints and timed practice with a pass
The second and third hints and the timed, runner-checked practice are part of a pass.