<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>most frequent Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/most-frequent/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/most-frequent/</link>
	<description></description>
	<lastBuildDate>Sat, 03 Mar 2018 07:21:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>most frequent Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/most-frequent/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 508. Most Frequent Subtree Sum</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-508-most-frequent-subtree-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-508-most-frequent-subtree-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 03 Mar 2018 06:45:31 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[most frequent]]></category>
		<category><![CDATA[subtree]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1919</guid>

					<description><![CDATA[<p>&#160; 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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-508-most-frequent-subtree-sum/">花花酱 LeetCode 508. Most Frequent Subtree Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>&nbsp;</p>
<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>
<p><b>Examples 1</b><br />
Input:</p><pre class="crayon-plain-tag">5
 /  \
2   -3</pre><p>return [2, -3, 4], since all the values happen only once, return all of them in any order.</p>
<p><b>Examples 2</b><br />
Input:</p><pre class="crayon-plain-tag">5
 /  \
2   -5</pre><p>return [2], since 2 happens twice, however -5 only occur once.</p>
<p><b>Note:</b> You may assume the sum of values in any subtree is in the range of 32-bit signed integer.</p>
<p>&nbsp;</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 18 ms
class Solution {
public:
  vector&lt;int&gt; findFrequentTreeSum(TreeNode* root) {
    unordered_map&lt;int, int&gt; freqs;
    int max_freq = -1;
    vector&lt;int&gt; ans;
    (void)treeSum(root, freqs, max_freq, ans);
    return ans;
  }
private:
  int treeSum(TreeNode* root, unordered_map&lt;int, int&gt;&amp; freqs, int&amp; max_freq, vector&lt;int&gt;&amp; ans) {
    if (!root) return 0;
    int sum = root-&gt;val + 
              treeSum(root-&gt;left, freqs, max_freq, ans) + 
              treeSum(root-&gt;right, freqs, max_freq, ans);
    int freq = ++freqs[sum];
    if (freq &gt; max_freq) {
      max_freq = freq;
      ans.clear();
    }
    if (freq == max_freq)
      ans.push_back(sum);
    return sum;
  }
};</pre><p>Python</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 72 ms (beats 100%)
"""
class Solution:
  def findFrequentTreeSum(self, root):   
    if not root: return []
    
    def treeSum(root):
      if not root: return 0
      s = root.val + treeSum(root.left) + treeSum(root.right)
      f[s] += 1   
      return s
    f = collections.Counter()
    treeSum(root)    
    max_freq = max(f.values())        
    return [s for s in f.keys() if f[s] == max_freq]</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-508-most-frequent-subtree-sum/">花花酱 LeetCode 508. Most Frequent Subtree Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/tree/leetcode-508-most-frequent-subtree-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
