leetcode 968. Binary Tree Cameras
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的更多相关文章
- 【LeetCode】968. Binary Tree Cameras 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- 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 ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [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 ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- LeetCode—— Invert Binary Tree
LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...
随机推荐
- docker的小技巧记录(如果使用了更多会继续添加)
docker小技巧 复制本地sql脚本到docker容器mysql中进行使用 # 找到容器 docker ps # 复制文件 cp ./xxx.sql container-id:/tmp/ # 进入容 ...
- [Doxygen].Docygen使用
转自:https://www.cnblogs.com/chenyang920/p/5732643.html Doxygen是一种开源跨平台的,以类似JavaDoc风格描述的文档系统,可以从一套归档源文 ...
- linux设备驱动程序--bus
linux 中bus驱动解析 总线(bus)是linux发展过程中抽象出来的一种设备模型,为了统一管理所有的设备,内核中每个设备都会被挂载在总线上,这个bus可以是对应硬件的bus(i2c bus.s ...
- 自动化渗透测试工具(Cobalt Strike)3.1 最新破解版
自动化渗透测试工具(Cobalt Strike)3.1 最新破解版[附使用教程] Cobalt Strike是一款专业的自动化渗透测试工具,它是图形化.可视化的,图形界面非常友好,一键傻瓜化使用MSF ...
- Dapper 一对多查询 one to many
参考文档:Dapper one to many Table public class Person { public int Id { get; set; } public string Name { ...
- 为什么要指定HashMap的容量?HashMap指定容量初始化后,底层Hash数组已经被分配内存了吗?
为什么要指定HashMap的容量? 首先创建HashMap时,指定容量比如1024后,并不是HashMap的size不是1024,而是0,插入多少元素,size就是多少: 然后如果不指定HashMap ...
- 安装GO
1.中文社区 下载地址 https://studygolang.com/dl 选择自己操作系统版本 2.找到适合你系统的版本下载,本人下载的是windows版本.也可以下载Source自己更 ...
- MSAA简介
https://www.cnblogs.com/gnagwang/archive/2010/04/20/1716006.html MSAA的全称是Microsoft Active Accessibil ...
- Linux计划作业练习
1.crontab -eu zh //每天晚上10天提醒用户可以去睡觉了 * */10 * * * go to sleep 2.查询crontab的工作内容 3.当crontab命令格式出错时 ...
- 【转载】java.util.ServiceConfigurationError: com.sun.tools.attach.spi.AttachProvider
https://blog.csdn.net/zqz_zqz/article/details/80922164 window上运行以下代码获取jvm进程: List<VirtualMachineD ...