{"id":4582,"date":"2018-12-31T00:46:26","date_gmt":"2018-12-31T08:46:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4582"},"modified":"2018-12-31T00:49:13","modified_gmt":"2018-12-31T08:49:13","slug":"leetcode-968-binary-tree-cameras","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-968-binary-tree-cameras\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 968. Binary Tree Cameras"},"content":{"rendered":"\n<p>Given a binary tree, we install cameras on the nodes of the tree.&nbsp;<\/p>\n\n\n\n<p>Each camera at&nbsp;a node can monitor&nbsp;<strong>its parent, itself, and its immediate children<\/strong>.<\/p>\n\n\n\n<p>Calculate the minimum number of cameras needed to monitor all nodes of the tree.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/29\/bst_cameras_01.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[0,0,null,0,0]\n<strong>Output: <\/strong>1\n<strong>Explanation: <\/strong>One camera is enough to monitor all nodes if placed as shown.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/29\/bst_cameras_02.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[0,0,null,0,null,0,null,null,0]\n<strong>Output: <\/strong>2\n<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.\n<\/pre>\n\n\n\n<p><br><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution:&nbsp;Greedy&nbsp;+&nbsp;Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(h)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">enum class State { NONE = 0, COVERED = 1, CAMERA = 2 };\n\nclass Solution {\npublic:\n  int minCameraCover(TreeNode* root) {        \n    if (dfs(root) == State::NONE) ++ans_;\n    return ans_;\n  }\nprivate:  \n  int ans_ = 0;\n  State dfs(TreeNode* root) {\n    if (!root) return State::COVERED;\n    State l = dfs(root->left);\n    State r = dfs(root->right);\n    if (l == State::NONE || r == State::NONE) {\n      ++ans_;\n      return State::CAMERA;\n    }\n    if (l == State::CAMERA || r == State::CAMERA)\n      return State::COVERED;    \n    return State::NONE;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree, we install cameras on the nodes of the tree.&nbsp; Each camera at&nbsp;a node can monitor&nbsp;its parent, itself, and its immediate children.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[450,217,17,28],"class_list":["post-4582","post","type-post","status-publish","format-standard","hentry","category-tree","tag-cover","tag-hard","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4582"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4582\/revisions"}],"predecessor-version":[{"id":4587,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4582\/revisions\/4587"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}