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 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]. - Every node has value 0.
做了很久,还是写不出来,看了网上的答案。确实很精妙。利用了返回值表达了三种状态,利用引用存储最后结果。
还有一点就是在放相机的时候,在递归函数里,父节点一定比节点有优势,能放父节点一定放父节点,拿叶节点来
举例,此时如果放叶节点,能影响到的点只有它本身和父节点,而父节点能影响到子节点,本身,和它的父节点。
所以这是一个贪心算法。
class Solution {
public:
int minCameraCover(TreeNode* root) {
int sum=;
if(dfs(root,sum)==) sum++;// if root is not monitored, we place an additional camera here
return sum;
}
int dfs(TreeNode * tr, int& sum){
if(!tr) return ;
int l=dfs(tr->left,sum), r=dfs(tr->right,sum);
if(l==||r==){// if at least 1 child is not monitored, we need to place a camera at current node
sum++;
return ;
}else if(l==||r==){// if at least 1 child has camera, the current node if monitored. Thus, we don't need to place a camera here
return ;
}else{// if both children are monitored but have no camera, we don't need to place a camera here. We place the camera at its parent node at the higher level.
return ;
}
return -;// this return statement won't be triggered
}
};
LC 968. Binary Tree Cameras的更多相关文章
- leetcode 968. Binary Tree Cameras
968. Binary Tree Cameras 思路:如果子节点只能覆盖到父节点.当前节点,但是父节点可以覆盖到他的父节点.子节点.当前节点,所以从叶子节点往上考虑 0代表子节点没有被覆盖 1代表子 ...
- 【LeetCode】968. Binary Tree Cameras 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [Swift]LeetCode968.监控二叉树 | Binary Tree Cameras
Given a binary tree, we install cameras on the nodes of the tree. Each camera at a node can monitor ...
- LC 94. Binary Tree Inorder Traversal
问题描述 Given a binary tree, return the inorder traversal of its nodes' values. (左 - 根 - 右) Example: In ...
- [LC] 314. Binary Tree Vertical Order Traversal
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LC] 156. Binary Tree Upside Down
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- [LC] 298. Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LC] 102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [LC] 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- ajax请求处理概要
/** *不关心参数传递与参数返回的形式. */ url = ctxPath + '/ccb/xxx '; $.get(url); $.post(url); /** * 常见形式. */ var ur ...
- 云计算与大数据实验:Hbase shell操作成绩表
[实验目的] 1)了解hbase服务 2)学会hbase shell命令操作成绩表 [实验原理] HBase是一个分布式的.面向列的开源数据库,它利用Hadoop HDFS作为其文件存储系统,利用Ha ...
- 虚拟机网络设置(NAT模式)
虚拟机网络设置(NAT模式) linux 1. 设置虚拟机网络 1.1. NAT子网设置 1.2. 网卡配置文件设置 1.3. 重启网络服务 1.4. 配置端口转发 2. 配置网络共享 预期想要搭建本 ...
- KMP算法的时间复杂度与next数组分析
一.什么是 KMP 算法 KMP 算法是一种改进的字符串匹配算法,用于判断一个字符串是否是另一个字符串的子串 二.KMP 算法的时间复杂度 O(m+n) 三.Next 数组 - KMP 算法的核心 K ...
- Codeforces Round #560 (Div. 3) Microtransactions
Codeforces Round #560 (Div. 3) F2. Microtransactions (hard version) 题意: 现在有一个人他每天早上获得1块钱,现在有\(n\)种商品 ...
- app开发-1
一.了解HBuilder HBuilder内封装了大量的书籍,极大方便了使用 官方文档: http://dev.dcloud.net.cn/mui/ui/ 关于布局: mhead 表头.mbody ...
- Spring Cloud 微服务:Eureka+Zuul+Ribbon+Hystrix+SpringConfig实现流程图
相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...
- 转 OJDBC驱动版本区别 [ojdbc14.jar,ojdbc5.jar跟ojdbc6.jar的区别]
OJDBC版本区别 [ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别] 在使用Oracle JDBC驱动时,有些问题你是不是通过替换不同版本的Oracle JDBC驱动来解 ...
- oracle 将与本端(name)联系的人取出
本人与其他所有人认识的SQL: 首先新建测试表 create table DIM_IA_TEST6 ( NAME ), OTHERNAME ) ) 插入数据 --如果没有重复的记录,则不用去重使用un ...
- .net core 多sdk 多版本 环境切换
在讲述.net core多版本之前,我们先理解一下.net core sdk与.net core runtime之前的联系与区别,根据官网的解释我们可以简单地理解为:sdk是在开发过程中进行使用,而r ...
Example 2: