<?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>balance Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/balance/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/balance/</link>
	<description></description>
	<lastBuildDate>Tue, 18 Dec 2018 23:28:10 +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>balance Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/balance/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 958. Check Completeness of a Binary Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-958-check-completeness-of-a-binary-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-958-check-completeness-of-a-binary-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 18 Dec 2018 22:59:02 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[balance]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4464</guid>

					<description><![CDATA[<p>Given a binary tree, determine if it is a&#160;complete binary tree. Definition of a complete binary tree from&#160;Wikipedia:In a complete binary tree every level, except&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-958-check-completeness-of-a-binary-tree/">花花酱 LeetCode 958. Check Completeness of a 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>Given a binary tree, determine if it is a&nbsp;<em>complete binary tree</em>.</p>



<p><strong>Definition of a complete binary tree from&nbsp;<a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank" rel="noreferrer noopener">Wikipedia</a>:</strong><br>In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2<sup>h</sup>&nbsp;nodes inclusive at the last level h.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-1.png" alt=""/></figure>



<pre class="crayon:false"><strong>Input: </strong>[1,2,3,4,5,6]<br><strong>Output: </strong>true<br><strong>Explanation: </strong>Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far left as possible.</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2018/12/15/complete-binary-tree-2.png" alt=""/></figure>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[1,2,3,4,5,null,7]<br><strong>Output: </strong>false<br><strong>Explanation: </strong>The node with value 7 isn't as far left as possible.</pre>



<p><strong>Note:</strong></p>



<ol><li>The tree will have between 1 and 100 nodes.</li></ol>



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



<p>Level order traversal, if any nodes appears after a missing node then the tree is not a perfect binary tree.</p>



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



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  bool isCompleteTree(TreeNode* root) {
    if (!root) return true;
    queue&lt;TreeNode&gt; q;
    q.push(root);
    bool missing = false;
    while (!q.empty()) {
      auto size = q.size();      
      while (size--) {
        auto node = q.front(); q.pop();        
        if (node) {
          if (missing) return false;
          q.push(node-&amp;gt;left);
          q.push(node-&amp;gt;right);
        } else {
          missing = true;
        }
      }      
    }
    return true;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def isCompleteTree(self, root):
    if not root: return True
    q = collections.deque([root])
    missing = False
    while q:
      size = len(q)    
      while size &gt; 0:
        size -= 1
        node = q.popleft()
        if node:
          if missing: return False
          q.append(node.left)
          q.append(node.right)
        else:
          missing = True
    return True</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-958-check-completeness-of-a-binary-tree/">花花酱 LeetCode 958. Check Completeness of a 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/tree/leetcode-958-check-completeness-of-a-binary-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 856. Score of Parentheses</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-856-score-of-parentheses/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-856-score-of-parentheses/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 27 Jun 2018 04:16:13 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[balance]]></category>
		<category><![CDATA[divide and conquer]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2934</guid>

					<description><![CDATA[<p>Problem Given a balanced parentheses string S, compute the score of the string based on the following rule: () has score 1 AB has score A + B, where&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-856-score-of-parentheses/">花花酱 LeetCode 856. Score of Parentheses</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/tiAaVfMcL9w?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Given a balanced parentheses string <code>S</code>, compute the score of the string based on the following rule:</p>
<ul>
<li><code>()</code> has score 1</li>
<li><code>AB</code> has score <code>A + B</code>, where A and B are balanced parentheses strings.</li>
<li><code>(A)</code> has score <code>2 * A</code>, where A is a balanced parentheses string.</li>
</ul>
<p>&nbsp;</p>
<div>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">"()"</span>
<strong>Output: </strong><span id="example-output-1">1</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">"(())"</span>
<strong>Output: </strong><span id="example-output-2">2</span>
</pre>
<div>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-3-1">"()()"</span>
<strong>Output: </strong><span id="example-output-3">2</span>
</pre>
<div>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-4-1">"(()(()))"</span>
<strong>Output: </strong><span id="example-output-4">6</span>
</pre>
<h1></h1>
<h1><img class="alignnone size-full wp-image-2941" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/856-ep198.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/856-ep198.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/856-ep198-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/856-ep198-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><img class="alignnone size-full wp-image-2940" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/856-ep198-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/856-ep198-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/856-ep198-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/856-ep198-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1>Solution1: Recursion</h1>
<p>Time complexity: O(n^2)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4ms
class Solution {
public:
  int scoreOfParentheses(string S) {
    return score(S, 0, S.length() - 1);
  }
private:
  int score(const string&amp; S, int l, int r) {    
    if (r - l == 1) return 1; // "()"
    int b = 0;
    for (int i = l; i &lt; r; ++i) {
      if (S[i] == '(') ++b;
      if (S[i] == ')') --b;
      if (b == 0) // balanced
        // score("(A)(B)") = score("(A)") + score("(B)")
        return score(S, l, i) + score(S, i + 1, r);    
    }
    // score("(A)") = 2 * score("A")
    return 2 * score(S, l + 1, r - 1); 
  }
};</pre><p>&nbsp;</p>
<h1><strong>Solution2: Counting</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  int scoreOfParentheses(string S) {
    int ans = 0;
    int d = -1;
    for (int i = 0; i &lt; S.length(); ++i) {
      d += S[i] == '(' ? 1 : -1;
      if (S[i] == '(' &amp;&amp; S[i + 1] == ')')
        ans += 1 &lt;&lt; d;
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
</div>
</div>
</div>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-856-score-of-parentheses/">花花酱 LeetCode 856. Score of Parentheses</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/string/leetcode-856-score-of-parentheses/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
