<?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>maximum Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/maximum/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/maximum/</link>
	<description></description>
	<lastBuildDate>Wed, 11 Jul 2018 01:56:29 +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>maximum Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/maximum/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 654. Maximum Binary Tree</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-654-maximum-binary-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-654-maximum-binary-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 05 Sep 2017 06:56:44 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[maximum]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[subarray]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=96</guid>

					<description><![CDATA[<p>&#160; Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-654-maximum-binary-tree/">花花酱 LeetCode 654. Maximum Binary Tree</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><iframe width="500" height="375" src="https://www.youtube.com/embed/oHnT9zTsXTg?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>&nbsp;</p>
<p>Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:</p>
<ol>
<li>The root is the maximum number in the array.</li>
<li>The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.</li>
<li>The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.</li>
</ol>
<p>Construct the maximum tree by the given array and output the root node of this tree.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree:

      6
    /   \
   3     5
    \    / 
     2  0   
       \
        1</pre><p><strong>Idea:</strong></p>
<p>Recursion</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/slide-654-1.png"><img class="alignnone size-full wp-image-97" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/slide-654-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/slide-654-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/slide-654-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/slide-654-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/slide-654-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution:</strong></p>
<p>With copy</p>
<p>Time complexity: O(nlogn) ~ O(n^2)</p>
<p>Space complexity: O(nlogn) ~ O(n^2)</p>
<p><span style="font-size: 1rem;">running time 79ms</span></p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:    
    TreeNode* constructMaximumBinaryTree(vector&lt;int&gt;&amp; nums) {
        if(nums.empty()) return nullptr;
        auto it = std::max_element(nums.begin(), nums.end());
        TreeNode* root=new TreeNode(*it);
        vector&lt;int&gt; left(nums.begin(), it);
        vector&lt;int&gt; right(it+1, nums.end());
        root-&gt;left = constructMaximumBinaryTree(left);
        root-&gt;right = constructMaximumBinaryTree(right);
        return root;
    }
}</pre><p>Without copy</p>
<p>Time complexity: O(nlogn) ~ O(n^2)</p>
<p>Space complexity: O(logn) ~ O(n)</p>
<p>running time 66ms</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:            
    TreeNode* constructMaximumBinaryTree(vector&lt;int&gt;&amp; nums) {
        return makeMBT(nums, 0, nums.size());
    }
private:
    TreeNode* makeMBT(const vector&lt;int&gt;&amp; nums, int begin, int end) {
        if(begin&gt;=end) return nullptr;
        auto it = std::max_element(nums.begin() + begin, nums.begin() + end);
        TreeNode* root=new TreeNode(*it);
        int index = it - nums.begin();
        root-&gt;left = makeMBT(nums, begin, index);
        root-&gt;right = makeMBT(nums, index+1, end);
        return root;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-654-maximum-binary-tree/">花花酱 LeetCode 654. Maximum Binary Tree</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/leetcode/leetcode-654-maximum-binary-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
