A company org chart is modeled as a binary tree: the root is the CEO and every node's children are that person's direct reports. Tier 0 is the CEO's tier, tier 1 holds the CEO's direct reports, and so on. Report the number of people on each tier, from the top tier down to the deepest.
Line 1: an integer n, the number of tokens on the next line.
Line 2: n space-separated tokens describing a binary tree in level-order (breadth-first). The first token is the root's value. Reading left to right, keep a queue of already-created nodes; for each node taken from the front of the queue, the next two tokens are its left child then its right child, where the token null marks a missing child. Only non-null children are added to the queue. Trailing null tokens for absent children at the deepest level may be omitted. Every node value is an integer.
A single line with the headcount of each tier from tier 0 downward, space-separated.
Example 1
Input
7 3 9 20 null null 15 7
Expected
1 2 2
Explanation
Tier 0 has just the root (3). Tier 1 has 9 and 20 (2 people). Tier 2 has 15 and 7 (2 people). Output: 1 2 2.
Example 2
Input
1 5
Expected
1
Explanation
A lone CEO: only tier 0 exists with 1 person. Output: 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →