<?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>balanced Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/balanced/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/balanced/</link>
	<description></description>
	<lastBuildDate>Sun, 15 Mar 2020 20:21:32 +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>balanced Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/balanced/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1382. Balance a Binary Search Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-1382-balance-a-binary-search-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-1382-balance-a-binary-search-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Mar 2020 08:39:29 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[balanced]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6500</guid>

					<description><![CDATA[<p>Given a binary search tree, return a&#160;balanced&#160;binary search tree with the same node values. A binary search tree is&#160;balanced&#160;if and only if&#160;the depth of the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1382-balance-a-binary-search-tree/">花花酱 LeetCode 1382. Balance a Binary Search 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[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1382. Balance a Binary Search Tree - 刷题找工作 EP315" width="500" height="375" src="https://www.youtube.com/embed/U24USYuOWzw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given a binary search tree, return a&nbsp;<strong>balanced</strong>&nbsp;binary search tree with the same node values.</p>



<p>A binary search tree is&nbsp;<em>balanced</em>&nbsp;if and only if&nbsp;the depth of the two subtrees of&nbsp;every&nbsp;node never differ by more than 1.</p>



<p>If there is more than one answer, return any of them.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/08/22/1515_ex1.png" alt=""/></figure>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/08/22/1515_ex1_out.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root = [1,null,2,null,3,null,4,null,null]
<strong>Output:</strong> [2,1,3,null,null,null,4]
<strong>Explanation:</strong> This is not the only correct answer, [3,1,4,null,2,null,null] is also correct.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li>The number of nodes in the tree is between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>10^4</code>.</li><li>The tree nodes will have distinct values between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>10^5</code>.</li></ul>



<h2><strong>Solution: Inorder + recursion</strong></h2>



<p>Use inorder traversal to collect a sorted array from BST. And then build a balanced BST from this sorted array in O(n) time.</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  TreeNode* balanceBST(TreeNode* root) {
    vector&lt;int&gt; vals;
    function&lt;void(TreeNode*)&gt; inorder = [&amp;](TreeNode* root) {
      if (!root) return;
      inorder(root-&gt;left);
      vals.push_back(root-&gt;val);
      inorder(root-&gt;right);
    };
    
    function&lt;TreeNode*(int, int)&gt; build = [&amp;](int l, int r) {
      if (l &gt; r) return (TreeNode*)nullptr;
      int m = l + (r - l) / 2;
      auto root = new TreeNode(vals[m]);
      root-&gt;left = build(l, m - 1);
      root-&gt;right = build(m + 1, r);
      return root;
    };
    
    inorder(root);
    return build(0, vals.size() - 1);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1382-balance-a-binary-search-tree/">花花酱 LeetCode 1382. Balance a Binary Search 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/tree/leetcode-1382-balance-a-binary-search-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1221. Split a String in Balanced Strings</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-1221-split-a-string-in-balanced-strings/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-1221-split-a-string-in-balanced-strings/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 14 Oct 2019 02:34:26 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[balanced]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5753</guid>

					<description><![CDATA[<p>Balanced&#160;strings are those who have equal quantity of &#8216;L&#8217; and &#8216;R&#8217; characters. Given a balanced string&#160;s&#160;split it in the maximum amount of balanced strings. Return&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1221-split-a-string-in-balanced-strings/">花花酱 LeetCode 1221. Split a String in Balanced Strings</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><em>Balanced</em>&nbsp;strings are those who have equal quantity of &#8216;L&#8217; and &#8216;R&#8217; characters.</p>



<p>Given a balanced string&nbsp;<code>s</code>&nbsp;split it in the maximum amount of balanced strings.</p>



<p>Return the maximum amount of splitted balanced strings.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "RLRRLLRLRL"
<strong>Output:</strong> 4
<strong>Explanation: </strong>s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "RLLLLRRRLR"
<strong>Output:</strong> 3
<strong>Explanation: </strong>s can be split into "RL", "LLLRRR", "LR", each substring contains same number of 'L' and 'R'.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "LLLLRRRR"
<strong>Output:</strong> 1
<strong>Explanation: </strong>s can be split into "LLLLRRRR".
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= s.length &lt;= 1000</code></li><li><code>s[i] = 'L' or 'R'</code></li></ul>



<h2><strong>Solution</strong>: <strong>Greedy</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int balancedStringSplit(string s) {
    int b = 0;
    int ans = 0;
    for (char c : s) {
      b += c == 'L' ? 1 : -1;
      if (b == 0) ++ans;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1221-split-a-string-in-balanced-strings/">花花酱 LeetCode 1221. Split a String in Balanced Strings</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/greedy/leetcode-1221-split-a-string-in-balanced-strings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
