forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-nodes-equal-to-average-of-subtree.py
58 lines (51 loc) · 1.64 KB
/
count-nodes-equal-to-average-of-subtree.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
# Time: O(n)
# Space: O(h)
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
pass
# dfs
class Solution(object):
def averageOfSubtree(self, root):
"""
:type root: Optional[TreeNode]
:rtype: int
"""
def iter_dfs(root):
result = 0
stk = [(1, (root, [0]*2))]
while stk:
step, args = stk.pop()
if step == 1:
node, ret = args
if not node:
continue
ret1, ret2 = [0]*2, [0]*2
stk.append((2, (node, ret1, ret2, ret)))
stk.append((1, (node.right, ret2)))
stk.append((1, (node.left, ret1)))
elif step == 2:
node, ret1, ret2, ret = args
ret[0] = ret1[0]+ret2[0]+node.val
ret[1] = ret1[1]+ret2[1]+1
result += int(ret[0]//ret[1] == node.val)
return result
return iter_dfs(root)
# Time: O(n)
# Space: O(h)
# dfs
class Solution2(object):
def averageOfSubtree(self, root):
"""
:type root: Optional[TreeNode]
:rtype: int
"""
def dfs(node):
if not node:
return [0]*3
left = dfs(node.left)
right = dfs(node.right)
return [left[0]+right[0]+node.val,
left[1]+right[1]+1,
left[2]+right[2]+int((left[0]+right[0]+node.val)//(left[1]+right[1]+1) == node.val)]
return dfs(root)[2]