<?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>travesal Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/travesal/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/travesal/</link>
	<description></description>
	<lastBuildDate>Tue, 06 Mar 2018 05:12:45 +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>travesal Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/travesal/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 515. Find Largest Value in Each Tree Row</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 06 Mar 2018 05:08:33 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[inorder]]></category>
		<category><![CDATA[max element]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[travesal]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1982</guid>

					<description><![CDATA[<p>题目大意：给你一棵树，返回每一层最大的元素的值。 You need to find the largest value in each row of a binary tree. Example: [crayon-663a2277e9a1c270701647/] Solution 1: Inorder traversal + depth info C++&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/">花花酱 LeetCode 515. Find Largest Value in Each Tree Row</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="question-description">
<p>题目大意：给你一棵树，返回每一层最大的元素的值。</p>
<p>You need to find the largest value in each row of a binary tree.</p>
<p><b>Example:</b></p><pre class="crayon-plain-tag">&lt;b&gt;Input:&lt;/b&gt; 

          1
         / \
        3   2
       / \   \  
      5   3   9 

&lt;b&gt;Output:&lt;/b&gt; [1, 3, 9]</pre><p>
</div>
<p>Solution 1: Inorder traversal + depth info</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 15 ms
class Solution {
public:
  vector&lt;int&gt; largestValues(TreeNode* root) {
    vector&lt;int&gt; ans;
    inorder(root, 0, ans);
    return ans;
  }
private:
  void inorder(TreeNode* root, int d, vector&lt;int&gt;&amp; ans) {
    if (root == nullptr) return;
    while (ans.size() &lt;= d) ans.push_back(INT_MIN);    
    inorder(root-&gt;left, d + 1, ans);
    ans[d] = max(ans[d], root-&gt;val);
    inorder(root-&gt;right, d + 1, ans);
  }
};</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 72 ms (beats 100%)
"""
class Solution:
  def largestValues(self, root):
    def inorder(root, d, ans):
      if not root: return ans
      if len(ans) &lt;= d: ans += [float('-inf')]
      inorder(root.left, d + 1, ans)
      if root.val &gt; ans[d]: ans[d] = root.val
      inorder(root.right, d + 1, ans)
      return ans
    
    return inorder(root, 0, [])</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/">花花酱 LeetCode 515. Find Largest Value in Each Tree Row</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-515-find-largest-value-in-each-tree-row/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
