-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path508.py
64 lines (56 loc) · 1.93 KB
/
508.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
__________________________________________________________________________________________________
sample 48 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
from collections import defaultdict
class Solution:
def findFrequentTreeSum(self, root: TreeNode) -> List[int]:
if root == None:
return
SUM = defaultdict(lambda:0)
def calculate(node):
sum = node.val
if node.left!=None:
sum += calculate(node.left)
if node.right != None:
sum += calculate(node.right)
SUM[sum] += 1
return sum
calculate(root)
max_val = max(SUM.values())
res = []
for a in SUM.keys():
if SUM[a]==max_val:
res.append(a)
return res
__________________________________________________________________________________________________
sample 16860 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def findFrequentTreeSum(self, root: TreeNode) -> List[int]:
if not root:
return []
ans = []
def dfs(root):
if not root:
return 0
left = dfs(root.left)
right = dfs(root.right)
mid = left + right + root.val
ans.append(mid)
return mid
dfs(root)
count = collections.Counter(ans)
max_occur = max(count.values())
#max_occur = max(count.items(), key = lambda x: x[1])
return [k for k,v in count.items() if v == max_occur]
__________________________________________________________________________________________________