forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-of-nodes-in-the-sub-tree-with-the-same-label.py
78 lines (71 loc) · 2.49 KB
/
number-of-nodes-in-the-sub-tree-with-the-same-label.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Time: O(n)
# Space: O(h)
class Solution(object):
def countSubTrees(self, n, edges, labels):
"""
:type n: int
:type edges: List[List[int]]
:type labels: str
:rtype: List[int]
"""
def iter_dfs(labels, adj, node, parent, result):
stk = [(1, (node, parent, [0]*26))]
while stk:
step, params = stk.pop()
if step == 1:
node, parent, ret = params
stk.append((4, (node, ret)))
stk.append((2, (node, parent, reversed(adj[node]), ret)))
elif step == 2:
node, parent, it, ret = params
child = next(it, None)
if not child or child == parent:
continue
ret2 = [0]*26
stk.append((2, (node, parent, it, ret)))
stk.append((3, (ret2, ret)))
stk.append((1, (child, node, ret2)))
elif step == 3:
ret2, ret = params
for k in xrange(len(ret2)):
ret[k] += ret2[k]
else:
node, ret = params
ret[ord(labels[node]) - ord('a')] += 1
result[node] += ret[ord(labels[node]) - ord('a')]
adj = [[] for _ in xrange(n)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
result = [0]*n
iter_dfs(labels, adj, 0, -1, result)
return result
# Time: O(n)
# Space: O(h)
import collections
class Solution2(object):
def countSubTrees(self, n, edges, labels):
"""
:type n: int
:type edges: List[List[int]]
:type labels: str
:rtype: List[int]
"""
def dfs(labels, adj, node, parent, result):
count = [0]*26
for child in adj[node]:
if child == parent:
continue
new_count = dfs(labels, adj, child, node, result)
for k in xrange(len(new_count)):
count[k] += new_count[k]
count[ord(labels[node]) - ord('a')] += 1
result[node] = count[ord(labels[node]) - ord('a')]
return count
adj = [[] for _ in xrange(n)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
result = [0]*n
dfs(labels, adj, 0, -1, result)
return result