968. Binary Tree Cameras

思路:如果子节点只能覆盖到父节点、当前节点,但是父节点可以覆盖到他的父节点、子节点、当前节点,所以从叶子节点往上考虑

0代表子节点没有被覆盖

1代表子节点被覆盖,但是子节点没有camera

2代表子节点被覆盖,子节点有camera

https://www.cnblogs.com/ethanhong/p/10200550.html

/**
* 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) {
int sum = ;
if(minCameraCover(root,sum) == )
sum++;
return sum;
}
int minCameraCover(TreeNode* root,int& sum){
if(!root)
return ;
int left = minCameraCover(root->left,sum);
int right = minCameraCover(root->right,sum);
if(left == || right == ){
sum++;
return ;
}
else if(left == || right == )
return ;
else
return ;
}
};

leetcode 968. Binary Tree Cameras的更多相关文章

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

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

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

  3. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  4. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  5. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  6. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  7. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  8. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  9. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

随机推荐

  1. JFrog杰蛙DevOps平台

    https://www.jfrog.com/confluence/display/XRAY/Welcome+to+JFrog+Xray

  2. c语言实现基本的数据结构(五) 单链队列

    #include <stdio.h> #include <tchar.h> #include <stdlib.h> #define MaxQueueSize 100 ...

  3. Linux core dump总结

    文章链接:https://www.cnblogs.com/Anker/p/6079580.html 1.前言 一直在从事linux下后台开发,经常与core文件打交道.还记得刚开始从事linux下开发 ...

  4. pandas的行列显示不全的解决方法

    pd.set_option('display.max_rows', 100) # 显示的最大行数(避免只显示部分行数据) pd.set_option('display.max_columns', 10 ...

  5. C++网站学习

    0.C++   一个专门做C++的网站 一.以下内容来自LEARN C++ 的<更好编写C++程序的5个建议>部分 1.C++的一些标准: Coding Standards C++ Cor ...

  6. 异常sql处理

    下面是在awr报告里面看到的有问题的sql,是9个变量的,在应用前台属于关联查询,在sqlplus里面手工执行检查实际执行情况如下: SELECT /*+ GATHER_PLAN_STATISTICS ...

  7. python基础语法7 闭包函数与装饰器

    闭包函数: 1.闭包函数必须在函数内部定义 2.闭包函数可以引用外层函数的名字 闭包函数是 函数嵌套.函数对象.名称空间与作用域 结合体. # 直接传参 def func(x): print(x) f ...

  8. XXE(外部实体注入攻击)

    利用XXE漏洞可以进行拒绝服务攻击.文件读取.命令代码执行.SQL(XSS)注入.内外扫描端口和入侵内网站点等,内网探测和入侵是利用XXE中支持的协议进行内网主机和端口的发现,可以理解为使用XXE进行 ...

  9. python--面向对象编程之学生选课系统练习

    1.系统目录结构 文件夹注解: bin--系统管理员和学生的主程序代码 config--系统的配置文件 db--系统的数据文件 admin--管理员的数据文件 student--学生的数据文件 lib ...

  10. @EnableFeignClients 客户端详细

    在Spring cloud应用中,当我们要使用feign客户端时,一般要做以下三件事情 : 1.使用注解@EnableFeignClients启用feign客户端: 示例 : @SpringBootA ...