当前位置:网站首页>LeetCode 968. 监控二叉树
LeetCode 968. 监控二叉树
2022-07-02 22:07:00 【从零开始的数据猿】
题目
给定一个二叉树,我们在树的节点上安装摄像头。
节点上的每个摄影头都可以监视其父对象、自身及其直接子对象。
计算监控树的所有节点所需的最小摄像头数量。
示例 1:

输入:[0,0,null,0,0]
输出:1
解释:如图所示,一台摄像头足以监控所有节点。
示例 2:

输入:[0,0,null,0,null,0,null,null,0]
输出:2
解释:需要至少两个摄像头来监视树的所有节点。 上图显示了摄像头放置的有效位置之一。
提示:
给定树的节点数的范围是 [1, 1000]。
每个节点的值都是 0。
解题思路
每个节点只可能三种状态:
0-未被监控
1-摄像头
2-已被监控
代码
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
//全局变量 摄像头个数
int res = 0;
public int minCameraCover(TreeNode root) {
if(judge(root) == 0)res++;
return res;
}
public int judge(TreeNode root){
//如果是空节点返回 已被监控
//这样叶子节点就是 未被监控
if(root == null)return 2;
//根据左右孩子判断本节点属性
int left = judge(root.left);
int right = judge(root.right);
//左右有一个 未被监控 则为摄像头
if(left == 0 || right == 0){
res++;
return 1;
}
//左右有一个 摄像头 则为已被监控
if(left == 1 || right == 1)return 2;
//剩下为左右都 已被监控 则为未被监控 交给上一层节点添加摄像头
return 0;
}
}
参考链接:https://leetcode.cn/problems/binary-tree-cameras/solution/tan-xin-by-lafe-d-to4v/
边栏推荐
- [leetcode] most elements [169]
- Commodity information management system (C language document version)
- Array advanced improvement
- 对象与对象变量
- Go condition variable
- P7072 [CSP-J2020] 直播获奖
- 服务器响应状态码
- Mathematical modeling -- graph and network models and methods (I)
- 《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
- 解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
猜你喜欢

Leetcode circular linked list (fast and slow pointer) code line by line interpretation

SimpleITK使用——4. 奇怪的问题

Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly

Dahua cloud native load balancing article - the passenger flow of small restaurants has increased

Simpleitk use - 3 Common operations

wait解决僵尸进程

【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?

数学建模——图与网络模型及方法(一)

UE4 game architecture learning notes

odoo13搭建医院HRP环境(详细步骤)
随机推荐
数据分析学习记录(二)---响应曲面法及Design-Expert的简单使用
go 4種單例模式
U++ 学习笔记 堆
杰理之充电拔出,无法触摸开机【篇】
Notes on key vocabulary in the English original of the biography of jobs (10) [chapter eight]
小鹏P7出事故,安全气囊未弹出,这正常吗?
Baidu AI Cloud - create a face recognition application
Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly
Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
go 4种单例模式
Server response status code
《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
用sentinel熔断比例阈值改不了,设置慢调用比例没效果
Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
高并发介绍及应对
Socket socket c/s end process
Simpleitk use - 4 Strange question
Uniapp wechat login returns user name and Avatar
Gas station [problem analysis - > problem conversion - > greed]
[foreign journal] sleep and weight loss