Skip to content

Commit d1948d3

Browse files
author
lightmen
committed
add most-frequent-subtree-sum[1].py
1 parent 40cca77 commit d1948d3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def __init__(self):
10+
self.c = collections.Counter()
11+
12+
def depth(self, root):
13+
if not root:
14+
return 0
15+
left = self.depth(root.left)
16+
right = self.depth(root.right)
17+
18+
total = root.val + left + right
19+
self.c[total] += 1
20+
return total
21+
22+
def findFrequentTreeSum(self, root):
23+
"""
24+
:type root: TreeNode
25+
:rtype: List[int]
26+
"""
27+
self.depth(root)
28+
maxfreq = max(self.c.values() + [None])
29+
30+
return [k for k, v in self.c.items() if v == maxfreq]

0 commit comments

Comments
 (0)