【LeetCode】968. Binary Tree Cameras 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/binary-tree-cameras/
题目描述
Given a binary tree, we install cameras on the nodes of the tree.
Each camera at a node can monitor its parent, itself, and its immediate children.
Calculate the minimum number of cameras needed to monitor all nodes of the tree.
Example 1:

Input: [0,0,null,0,0]
Output: 1
Explanation: One camera is enough to monitor all nodes if placed as shown.
Example 2:

Input: [0,0,null,0,null,0,null,null,0]
Output: 2
Explanation: 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.
Note:
- The number of nodes in the given tree will be in the range
[1, 1000]. Everynode has value 0.
题目大意
如果放置一个摄像机,能覆盖当前节点、两个孩子节点、父亲节点。求最少的放置相机的个数。
解题方法
首先分析,每种节点能被多少种方案覆盖:
- 树中间的节点可以被当前节点、两个孩子节点、父亲节点四种方式覆盖。
- 根节点,可以被当前节点、两个孩子节点三种方式覆盖。
- 如果是叶子节点,可以被当前节点和父亲节点两种方式覆盖。
综上,我们最好的方案应该是从下向上,先设置叶子节点,然后移除所有覆盖的节点;再重复这个步骤。
具体方法是:
我们定义了一个函数dfs,
- 如果这个节点是叶子节点,返回0
- 如果这个节点是叶子节点的父节点,并且这个节点应该放相机,返回1
- 如果这个节点被子节点覆盖了,并且这个节点没有相机,返回2.
对于每个节点的话,
- 如果这个节点有子节点,并且这个子节点是叶子节点(节点0),那么当前节点需要相机0;
- 如果这个节点有子节点,并且这个子节点放置了相机(节点1),那么当前节点被覆盖了;
如果节点需要相机,那么对返回结果+1,并且返回1.
如果节点被覆盖了,返回2.
否则返回0.
C++代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minCameraCover(TreeNode* root) {
if (dfs(root) == State::NONE)
++ans_;
return ans_;
}
private:
enum class State {NONE = 0, COVERED = 1, CAMERA = 2};
int ans_ = 0;
State dfs(TreeNode* root) {
if (!root) return State::COVERED;
State l = dfs(root->left);
State r = dfs(root->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;
}
};
参考资料:https://leetcode.com/problems/binary-tree-cameras/discuss/211180/JavaC%2B%2BPython-Greedy-DFS
日期
2019 年 1 月 7 日 —— 新的一周开始啦啦啊
【LeetCode】968. Binary Tree Cameras 解题报告(C++)的更多相关文章
- leetcode 968. Binary Tree Cameras
968. Binary Tree Cameras 思路:如果子节点只能覆盖到父节点.当前节点,但是父节点可以覆盖到他的父节点.子节点.当前节点,所以从叶子节点往上考虑 0代表子节点没有被覆盖 1代表子 ...
- 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- LC 968. Binary Tree Cameras
Given a binary tree, we install cameras on the nodes of the tree. Each camera at a node can monitor ...
- 【leetcode 968. 监控二叉树】解题报告
解题思路: 由于叶子节点一定不要安装监视器,这样才能使总监视器数量比较少,因此需要从下往上进行判断当前节点的状态(共:3种状态): 0: 当前节点安装了监视器 1: 当前节点可观,但没有安装监视器 2 ...
随机推荐
- 【Proteogenomis】工具评述
2015年的综述文章,marker一下,供以后比较试用.蛋白质基因组学研究中的质谱仪与生物信息学方法. 针对蛋白质基因组学的分析研究,总结了质谱仪和蛋白质基因组学流程分析软件的选择,重点评述了常用的生 ...
- eggNOG 5.0数据库介绍
目录 1. eggNOG简介 2. eggNOG-Mapper注释原理 3. eggNOG 5.0数据资源 4. eggNOG-Mapper使用 5. NOG.KOG.COG.KEGG.GO区别? 1 ...
- Python基础之列表内置方法
目录 1. 列表 1.1 序列 1.2 通用的序列操作 1.3 列表的基本操作 1.4 列表方法 1. 列表 数据结构:以某种方式(如通过编号)组合起来的元素(如数,字符乃至其他数据结构)集合. 在p ...
- urllib的基本使用介绍
1. urllib中urlopen的基本使用介绍 1 ### urllib中urlopen的基本使用介绍 2 3 ## urlopen的基本用法(GET请求) 4 import urllib.requ ...
- 解决UE4项目编辑器启动时出现LogLinker: Warning: Failed to load '/Game/FirstPersonBP/FirstPersonOverview': Can't find file.
UE4版本:4.24.3源码编译版本 Windows10 + VS2019环境 LogLinker: Warning: Failed to load '/Game/FirstPersonBP/Firs ...
- LeetCode最富有客户的资产总量
最富有客户的资产总量 题目描述 给你一个 m * n 的整数网格 accounts,其中 account[i][j]是第 i 位客户在第 j 家银行托管的资产数量.返回最富有客户所拥有的资产总量. 客 ...
- A Child's History of England.31
The English in general were on King Henry's side, though many of the Normans were on Robert's. But t ...
- Scala(六)【模式匹配】
目录 一.基本语法 二.匹配固定值 三.守卫 四.匹配类型 五.匹配集合 1.Array 2.List 3.元祖 4.对象和样例类 六.偏函数 七.赋值匹配 八.for循环匹配 一.基本语法 在匹配某 ...
- 零基础学习java------day11------常用API---Object、Scanner、String、StringBufer/StringBuilder
API概述 API(application Programming Interface, 应用程序编程接口),是一些预先定义的函数.目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力, ...
- C++ 类型转换(C风格的强制转换):
转https://www.cnblogs.com/Allen-rg/p/6999360.html C++ 类型转换(C风格的强制转换): 在C++基本的数据类型中,可以分为四类:整型,浮点型,字符型, ...