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 ...
随机推荐
- 二十七、mysql如何确保数据不丢失?有几点值得我们借鉴
本篇文章我们先来看一下mysql是如何确保数据不丢失的,通过本文我们可以了解mysql内部确保数据不丢失的原理,学习里面优秀的设计要点,然后我们再借鉴这些优秀的设计要点进行实践应用,加深理解. 预备知 ...
- 【HCIA Gauss】学习汇总-数据库管理(事务 权限 审计 OBDC JDBC)-6
事务控制事务提交 commit事务回滚 rollback savepoint 用于事务设置保存点 ----> savepoint s1 / savepoint s2 rollback to sa ...
- composer.json详解
composer.json 架构:https://docs.phpcomposer.com/04-schema.html#homepage composer.json 完全解析:https://lea ...
- 使用Cloudera Manager部署Kafka消息队列
使用Cloudera Manager部署Kafka消息队列 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.下载需要安装的Kafka版本 1>.查看Cloudera Dis ...
- 安恒西湖论剑线下上午CTF部分题目WP
简单的做了两个题,一道逆向,一道misc,其他题目,因为博主上课,时间不太够,复现时间也只有一天,后面的会慢慢补上 先说RE1,一道很简单的win32逆向,跟踪主函数,R或者TAB按几下, 根据esp ...
- beta版本——第七次冲刺
第七次冲刺 (1)SCRUM部分☁️ 成员描述: 姓名 李星晨 完成了哪个任务 编写个人信息修改界面的js 花了多少时间 3h 还剩余多少时间 0h 遇到什么困难 密码验证部分出现问题 这两天解决的进 ...
- spring cloud (八) Config client 和项目公共配置
1 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- java调用c++库
c++ 写的库 jni封装一层 才可以给 java调用
- danci6
current 英 ['kʌr(ə)nt] 美 ['kɝənt] adj. 现在的:流通的,通用的:最近的:草写的 n. (水,气,电)流:趋势:涌流 n. (Current)人名:(英)柯伦特
- VMware共享本地文件
最后可以在这里找到
Example 2: