Skip to content

Commit bb00d8f

Browse files
Number of Nodes in the Sub-Tree With the Same Label
1 parent 6a90986 commit bb00d8f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def countSubTrees(self, n: int, edges: List[List[int]], labels: str) -> List[int]:
3+
def helper(node: int):
4+
count = Counter()
5+
if node not in seen:
6+
count[labels[node]] += 1
7+
seen.add(node)
8+
for child in g.get(node, []):
9+
count += helper(child)
10+
result[node] = count[labels[node]]
11+
return count
12+
g, result, seen = defaultdict(list), [0] * n, set()
13+
for a, b in edges:
14+
g[a] += [b]
15+
g[b] += [a]
16+
helper(0)
17+
return result

0 commit comments

Comments
 (0)