Category leaf counts

Write codeEasy15 min

Your task

A storefront keeps its category catalog as a tree. Entry i of children lists the indexes of the direct subcategories of category i, in the order a merchandiser arranged them, and category 0 is the catalog root. A category with no subcategories of its own is a leaf, which is where products actually hang. The navigation bar shows the root's direct subcategories, and each one needs a badge counting the leaf categories underneath it. Return one count per direct subcategory of the root, in the order those subcategories appear in the root's own entry. A subcategory with nothing beneath it is itself a leaf and counts as 1. Indexes can point anywhere in the list, so a subcategory is not guaranteed to sit after its parent. Return an empty list when children is empty or when the root has no subcategories.

Examples

taxonomy_branch_leaf_counts(children: list[list[int]]) -> list[int]
Example 1
Inputtaxonomy_branch_leaf_counts([[1, 2], [3, 4], [], [], []])
Output[2, 1]
Example 2
Inputtaxonomy_branch_leaf_counts([[2, 1], [], [3, 4], [], []])
Output[2, 1]
Example 3
Inputtaxonomy_branch_leaf_counts([[1], [2], [3], []])
Output[1]