<?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>univalue Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/univalue/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/univalue/</link>
	<description></description>
	<lastBuildDate>Sun, 30 Dec 2018 20:41:36 +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>univalue Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/univalue/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 965. Univalued Binary Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-965-univalued-binary-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-965-univalued-binary-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Dec 2018 20:07:33 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[tree]]></category>
		<category><![CDATA[univalue]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4563</guid>

					<description><![CDATA[<p>Problem A binary tree is&#160;univalued&#160;if every node in the tree has the same value. Return&#160;true&#160;if and only if the given tree is univalued. Example 1:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-965-univalued-binary-tree/">花花酱 LeetCode 965. Univalued 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[
<h2>Problem</h2>



<p>A binary tree is&nbsp;<em>univalued</em>&nbsp;if every node in the tree has the same value.</p>



<p>Return&nbsp;<code>true</code>&nbsp;if and only if the given tree is univalued.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2018/12/28/unival_bst_1.png" alt=""/></figure>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[1,1,1,1,1,null,1]
<strong>Output: </strong>true
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2018/12/28/unival_bst_2.png" alt=""/></figure>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[2,2,2,5,2]
<strong>Output: </strong>false
</pre>



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



<ol><li>The number of nodes in the given tree will be in the range&nbsp;<code>[1, 100]</code>.</li><li>Each node&#8217;s value will be an integer in the range&nbsp;<code>[0, 99]</code>.</li></ol>



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



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



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

<pre class="crayon-plain-tag">// Author: Huahua, running time: 0 ms
class Solution {
public:
  bool isUnivalTree(TreeNode* root) {
    if (!root) return true;
    if (root-&gt;left &amp;&amp; root-&gt;val != root-&gt;left-&gt;val) return false;
    if (root-&gt;right &amp;&amp; root-&gt;val != root-&gt;right-&gt;val) return false;
    return isUnivalTree(root-&gt;left) &amp;&amp; isUnivalTree(root-&gt;right);
  }
};</pre>

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

<pre class="crayon-plain-tag"># Author: Huahua, running time: 68 ms
class Solution:
  def isUnivalTree(self, root):
    if not root: return True
    if root.left and root.left.val != root.val: return False
    if root.right and root.right.val != root.val: return False
    return self.isUnivalTree(root.left) and self.isUnivalTree(root.right)</pre>
</div></div>



<h2><strong>Related Problems</strong></h2>



<ul><li><a href="https://zxi.mytechroad.com/blog/tree/leetcode-687-longest-univalue-path/">https://zxi.mytechroad.com/blog/tree/leetcode-687-longest-univalue-path/</a></li><li><a href="https://zxi.mytechroad.com/blog/tree/leetcode-124-binary-tree-maximum-path-sum/">https://zxi.mytechroad.com/blog/tree/leetcode-124-binary-tree-maximum-path-sum/</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-965-univalued-binary-tree/">花花酱 LeetCode 965. Univalued 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-965-univalued-binary-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
