作者: 负雪明烛
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:

  1. The number of nodes in the given tree will be in the range [1, 1000].
  2. Every node has value 0.

题目大意

如果放置一个摄像机,能覆盖当前节点、两个孩子节点、父亲节点。求最少的放置相机的个数。

解题方法

首先分析,每种节点能被多少种方案覆盖:

  1. 树中间的节点可以被当前节点、两个孩子节点、父亲节点四种方式覆盖。
  2. 根节点,可以被当前节点、两个孩子节点三种方式覆盖。
  3. 如果是叶子节点,可以被当前节点和父亲节点两种方式覆盖。

综上,我们最好的方案应该是从下向上,先设置叶子节点,然后移除所有覆盖的节点;再重复这个步骤。

具体方法是:

我们定义了一个函数dfs,

  1. 如果这个节点是叶子节点,返回0
  2. 如果这个节点是叶子节点的父节点,并且这个节点应该放相机,返回1
  3. 如果这个节点被子节点覆盖了,并且这个节点没有相机,返回2.

对于每个节点的话,

  1. 如果这个节点有子节点,并且这个子节点是叶子节点(节点0),那么当前节点需要相机0;
  2. 如果这个节点有子节点,并且这个子节点放置了相机(节点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++)的更多相关文章

  1. leetcode 968. Binary Tree Cameras

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

  2. 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)

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

  3. 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)

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

  4. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

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

  5. 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)

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

  6. 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)

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

  7. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

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

  9. 【leetcode 968. 监控二叉树】解题报告

    解题思路: 由于叶子节点一定不要安装监视器,这样才能使总监视器数量比较少,因此需要从下往上进行判断当前节点的状态(共:3种状态): 0: 当前节点安装了监视器 1: 当前节点可观,但没有安装监视器 2 ...

随机推荐

  1. nginx_rewrite

    介绍: 和apache等web服务软件一样,rewrite的组要功能是实现RUL地址的重定向.Nginx的rewrite功能需要PCRE软件的支持,即通过perl兼容正则表达式语句进行规则匹配的.默认 ...

  2. A Child's History of England.27

    Then, the Red King went over to Normandy, where the people suffered greatly under the loose rule of ...

  3. 在idea的java开发中字符串length()方法获取长度与赋值不符的问题

    最近在开发中用到length()方法获取中文字符串的长度,发现获得的长度与实际不符.比如个String类型赋值为"中",但获取长度却是2. 这让我百思不得其解,后来突然想起来我在研 ...

  4. Hadoop【MR的分区、排序、分组】

    [toc] 一.分区 问题:按照条件将结果输出到不同文件中 自定义分区步骤 1.自定义继承Partitioner类,重写getPartition()方法 2.在job驱动Driver中设置自定义的Pa ...

  5. 利用extern共享全局变量

    方法: 在xxx.h中利用extern关键字声明全局变量 extern int a; 在xxx.cpp中#include<xxx.h> 再定义 int a; 赋不赋初值无所谓,之后该全局变 ...

  6. spring认证的一些核心类

    SecurityContextHolder, to provide access to the SecurityContext. SecurityContext: to hold the Authen ...

  7. html框架frame iframe

    框架 通过使用框架,你可以在同一个浏览器窗口中显示不止一个页面.没分HTML文档称作一个框架. 缺点: 开发人员必须同时跟踪更多的HTML文档 很难打印整张页面 框架结构标签(<frameset ...

  8. Camera、音频录制与Vitamio框架

    一.Camera 1.概述 Android框架包含了各种相机哥相机功能的支持,是你可以在应用中捕获图像和视频. 在应用能使用设备上的相机之前,先想一想将来会如何使用此硬件: (1)Camera  应该 ...

  9. 【Linux】【Services】【nfs】nfs安装与配置

    1. 概念 1.1. NFS:Network File System,传统意义上,文件系统在内核中实现. 1.2. RPC:Remote Procedure Call protocol,远程过程调用, ...

  10. 【Java 8】Predicate详解

    一.java.util.function.Predicate 这里类是java自带主要广泛用在支持lambda表达式的API中. 1.接口源码 @FunctionalInterface public ...