当前位置:网站首页>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/
边栏推荐
- NC24325 [USACO 2012 Mar S]Flowerpot
- 牛客网:最大子矩阵
- 杰理之充电拔出,无法触摸开机【篇】
- Local dealers play the community group purchase mode and share millions of operations
- The kth largest element in the [leetcode] array [215]
- 【板栗糖GIS】global mapper 如何通过dsm批量制作贴地等高线
- How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
- 百度智能云-创建人脸识别应用
- uniapp微信登录返显用户名和头像
- Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly
猜你喜欢
Dynamic memory allocation (malloc calloc realloc free)
加油站[问题分析->问题转换->贪心]
kubernetes 使用主机名将 pod 分配在指定节点上
Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
世界环境日 | 周大福用心服务推动减碳环保
Socket socket c/s end process
Source code analysis - lightweight asynchronous crawler framework Ruia
【板栗糖GIS】global mapper 如何通过dsm批量制作贴地等高线
【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?
随机推荐
U++ 学习笔记 堆
Il n'est pas nécessaire d'appuyer longtemps sur la fonction de démarrage pour modifier Jelly [chapitre]
牛客网:龙与地下城游戏
全面解析分享购商业模式逻辑?分享购是如何赋能企业
【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
Zhong Xuegao responded that the product will not melt for 1 hour: it contains solid components and cannot melt into water
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
Baidu AI Cloud - create a face recognition application
PHP optimizes SQL queries in foreach
用sentinel熔断比例阈值改不了,设置慢调用比例没效果
go 4种单例模式
Utilisation de simpletk - 4. Question étrange
杰理之、产线装配环节【篇】
[leetcode] number of palindromes [9]
JS solution for obtaining the width and height of hidden elements whose display is none
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
uniapp微信登录返显用户名和头像
[LeetCode] 存在重复元素【217】
Storage unit conversion
`Usage of ${}`