-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path060_findFrequentTreeSum.py
More file actions
49 lines (45 loc) · 1.53 KB
/
060_findFrequentTreeSum.py
File metadata and controls
49 lines (45 loc) · 1.53 KB
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
# 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]:
array = []
hashMap = dict()
maxNum = 0
returnArray = []
def _helper(root):
if root == None:
return None
left = _helper(root.left)
right = _helper(root.right)
if left == None and right == None:
array.append(root.val)
return root
elif left == None:
array.append(root.val + right.val)
return TreeNode(root.val + right.val)
elif right == None:
array.append(root.val + left.val)
return TreeNode(root.val + left.val)
else:
array.append(left.val + right.val + root.val)
return TreeNode(left.val + right.val + root.val)
_helper(root)
for i in range(len(array)):
if array[i] not in hashMap:
hashMap[array[i]] = 1
else:
hashMap[array[i]] += 1
for vals in hashMap:
if hashMap[vals] > maxNum:
maxNum = hashMap[vals]
print(hashMap)
print(array)
print(maxNum)
for vals in hashMap:
if hashMap[vals] == maxNum:
returnArray.append(vals)
return returnArray