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:

    1. The number of nodes in the given tree will be in the range [1, 1000].
    2. 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的更多相关文章

  1. leetcode 968. Binary Tree Cameras

    968. Binary Tree Cameras 思路:如果子节点只能覆盖到父节点.当前节点,但是父节点可以覆盖到他的父节点.子节点.当前节点,所以从叶子节点往上考虑 0代表子节点没有被覆盖 1代表子 ...

  2. 【LeetCode】968. Binary Tree Cameras 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. [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  ...

  4. LC 94. Binary Tree Inorder Traversal

    问题描述 Given a binary tree, return the inorder traversal of its nodes' values. (左 - 根 - 右) Example: In ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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, ...

  9. [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 ...

随机推荐

  1. pycharm 专业注册

    pycharm的bin目录下pycharm.exe.vmoptions和pycharm64.exe.vmoptions两个配置文件添加 这个路径  -javaagent:E:\PyCharm 2017 ...

  2. 【Spring Boot】Spring Boot之自定义拦截器

    一.拦截器的作用 将通用的代码抽取出来,达到复用的效果.比如可以用来做日志记录.登录判断.权限校验等等 二.如何实现自定义拦截器 1)创建自定义拦截器类并实现HandlerInterceptor类 / ...

  3. Cloudera Certified Associate Administrator案例之Install篇

    Cloudera Certified Associate Administrator案例之Install篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.创建主机模板(为了给主 ...

  4. [转载]Mysql数据库千万级数据处理优化

    转载:http://blog.sina.com.cn/s/blog_6dcd17320100tm6o.html 1. 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by ...

  5. Windows定时任务下载linux服务器批量文件到本地

    编写批文件 1.1 编写main.bat文件 E: cd logs ftp -n -s:"E:\logs\mget.bat" 1.2 编写mget.bat文件 open ip地址 ...

  6. oracle 字符串分隔去重函数

    create or replaceFUNCTION "SF_SPLIT_ACCOUNT_ID_LIST" ( account_id_list IN VARCHAR2)RETURN ...

  7. kafka题目

    1. Kafka的用途有哪些?使用场景如何?2. Kafka中的ISR.AR又代表什么?ISR的伸缩又指什么3. Kafka中的HW.LEO.LSO.LW等分别代表什么?4. Kafka中是怎么体现消 ...

  8. Linux下串口操作

    一.Linux下访问串口 串口位置:/dev/tty** 在Linux系统中,串口设备是通过串口终端设备文件来访问的,也就是通过访问/dev/ttyS0./dev/ttyS1./dev/ttyS2./ ...

  9. Dubbo源码分析:Server

    Server接口是开启一个socket服务.服务实现有netty,mina,grizzly的. 抽象时序图 获取NettyServer时序图 Transporter类图 Server 类图

  10. jQuery - 添加元素append/prepend和after/before的区别

    append <p> <span class="s1">s1</span> </p> <script> $(" ...