We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 40cca77 commit d1948d3Copy full SHA for d1948d3
python/hash-table/most-frequent-subtree-sum[1].py
@@ -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