-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-the-number-of-good-nodes.py
82 lines (77 loc) · 2.35 KB
/
count-the-number-of-good-nodes.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
79
80
81
82
# Time: O(n)
# Space: O(h)
# iterative dfs
class Solution(object):
def countGoodNodes(self, edges):
"""
:type edges: List[List[int]]
:rtype: int
"""
def iter_dfs():
result = 0
stk = [(1, (0, -1, [0]))]
while stk:
step, args = stk.pop()
if step == 1:
u, p, ret = args
l, valid = [0], [True]
stk.append((4, (ret, valid)))
stk.append((2, (u, p, 0, ret, l, valid)))
elif step == 2:
u, p, i, ret, l, valid = args
if i == len(adj[u]):
continue
stk.append((2, (u, p, i+1, ret, l, valid)))
v = adj[u][i]
if v == p:
continue
new_ret = [0]
stk.append((3, (new_ret, ret, l, valid)))
stk.append((1, (v, u, new_ret)))
elif step == 3:
new_ret, ret, l, valid = args
ret[0] += new_ret[0]
l[0] += 1
if new_ret[0]*l[0] != ret[0]:
valid[0] = False
elif step == 4:
ret, valid = args
if valid[0]:
result += 1
ret[0] += 1
return result
adj = [[] for _ in xrange(len(edges)+1)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
return iter_dfs()
# Time: O(n)
# Space: O(h)
# dfs
class Solution2(object):
def countGoodNodes(self, edges):
"""
:type edges: List[List[int]]
:rtype: int
"""
def dfs(u, p):
total = l = 0
valid = True
for v in adj[u]:
if v == p:
continue
cnt = dfs(v, u)
total += cnt
l += 1
if l*cnt != total:
valid = False
if valid:
result[0] += 1
return total+1
adj = [[] for _ in xrange(len(edges)+1)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
result = [0]
dfs(0, -1)
return result[0]