<?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>cover Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/cover/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/cover/</link>
	<description></description>
	<lastBuildDate>Mon, 31 Dec 2018 08:49:13 +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>cover Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/cover/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 968. Binary Tree Cameras</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-968-binary-tree-cameras/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-968-binary-tree-cameras/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 31 Dec 2018 08:46:26 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[cover]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4582</guid>

					<description><![CDATA[<p>Given a binary tree, we install cameras on the nodes of the tree.&#160; Each camera at&#160;a node can monitor&#160;its parent, itself, and its immediate children.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-968-binary-tree-cameras/">花花酱 LeetCode 968. Binary Tree Cameras</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, we install cameras on the nodes of the tree.&nbsp;</p>



<p>Each camera at&nbsp;a node can monitor&nbsp;<strong>its parent, itself, and its immediate children</strong>.</p>



<p>Calculate the minimum number of cameras needed to monitor all nodes of the tree.</p>



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



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[0,0,null,0,0]
<strong>Output: </strong>1
<strong>Explanation: </strong>One camera is enough to monitor all nodes if placed as shown.
</pre>



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



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[0,0,null,0,null,0,null,null,0]
<strong>Output: </strong>2
<strong>Explanation:</strong> At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
</pre>



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



<ol><li>The number of nodes in the given tree will be in the range&nbsp;<code>[1, 1000]</code>.</li><li><strong>Every</strong>&nbsp;node has value 0.</li></ol>



<h2><strong>Solution:&nbsp;Greedy&nbsp;+&nbsp;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">enum class State { NONE = 0, COVERED = 1, CAMERA = 2 };

class Solution {
public:
  int minCameraCover(TreeNode* root) {        
    if (dfs(root) == State::NONE) ++ans_;
    return ans_;
  }
private:  
  int ans_ = 0;
  State dfs(TreeNode* root) {
    if (!root) return State::COVERED;
    State l = dfs(root-&gt;left);
    State r = dfs(root-&gt;right);
    if (l == State::NONE || r == State::NONE) {
      ++ans_;
      return State::CAMERA;
    }
    if (l == State::CAMERA || r == State::CAMERA)
      return State::COVERED;    
    return State::NONE;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-968-binary-tree-cameras/">花花酱 LeetCode 968. Binary Tree Cameras</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-968-binary-tree-cameras/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
