{"id":1919,"date":"2018-03-02T22:45:31","date_gmt":"2018-03-03T06:45:31","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1919"},"modified":"2018-03-02T23:21:51","modified_gmt":"2018-03-03T07:21:51","slug":"leetcode-508-most-frequent-subtree-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-508-most-frequent-subtree-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 508. Most Frequent Subtree Sum"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order.<\/p>\n<p><b>Examples 1<\/b><br \/>\nInput:<\/p>\n<pre>  5\r\n \/  \\\r\n2   -3\r\n<\/pre>\n<p>return [2, -3, 4], since all the values happen only once, return all of them in any order.<\/p>\n<p><b>Examples 2<\/b><br \/>\nInput:<\/p>\n<pre>  5\r\n \/  \\\r\n2   -5\r\n<\/pre>\n<p>return [2], since 2 happens twice, however -5 only occur once.<\/p>\n<p><b>Note:<\/b>\u00a0You may assume the sum of values in any subtree is in the range of 32-bit signed integer.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 18 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; findFrequentTreeSum(TreeNode* root) {\r\n    unordered_map&lt;int, int&gt; freqs;\r\n    int max_freq = -1;\r\n    vector&lt;int&gt; ans;\r\n    (void)treeSum(root, freqs, max_freq, ans);\r\n    return ans;\r\n  }\r\nprivate:\r\n  int treeSum(TreeNode* root, unordered_map&lt;int, int&gt;&amp; freqs, int&amp; max_freq, vector&lt;int&gt;&amp; ans) {\r\n    if (!root) return 0;\r\n    int sum = root-&gt;val + \r\n              treeSum(root-&gt;left, freqs, max_freq, ans) + \r\n              treeSum(root-&gt;right, freqs, max_freq, ans);\r\n    int freq = ++freqs[sum];\r\n    if (freq &gt; max_freq) {\r\n      max_freq = freq;\r\n      ans.clear();\r\n    }\r\n    if (freq == max_freq)\r\n      ans.push_back(sum);\r\n    return sum;\r\n  }\r\n};<\/pre>\n<p>Python<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 72 ms (beats 100%)\r\n\"\"\"\r\nclass Solution:\r\n  def findFrequentTreeSum(self, root):   \r\n    if not root: return []\r\n    \r\n    def treeSum(root):\r\n      if not root: return 0\r\n      s = root.val + treeSum(root.left) + treeSum(root.right)\r\n      f[s] += 1   \r\n      return s\r\n    f = collections.Counter()\r\n    treeSum(root)    \r\n    max_freq = max(f.values())        \r\n    return [s for s in f.keys() if f[s] == max_freq]<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[82,226,225,62],"class_list":["post-1919","post","type-post","status-publish","format-standard","hentry","category-tree","tag-hashtable","tag-most-frequent","tag-subtree","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1919"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1919\/revisions"}],"predecessor-version":[{"id":1924,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1919\/revisions\/1924"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}