当前位置:网站首页>LeetCode 968. Monitor binary tree
LeetCode 968. Monitor binary tree
2022-07-02 22:54:00 【Data ape from zero】
subject
Given a binary tree , We put cameras on the nodes of the tree .
Each camera head on a node can monitor its parent object 、 Itself and its immediate children .
Calculate the minimum number of cameras required for all nodes of the monitoring tree .
Example 1:
Input :[0,0,null,0,0]
Output :1
explain : As shown in the figure , One camera is enough to monitor all nodes .
Example 2:
Input :[0,0,null,0,null,0,null,null,0]
Output :2
explain : You need at least two cameras to monitor all the nodes in the tree . The image above shows one of the effective locations for the camera to be placed .
Tips :
The range of the number of nodes in a given tree is [1, 1000].
The value of each node is 0.
Their thinking
Each node can only have three states :
0- Not monitored
1- camera
2- Monitored
Code
/** * 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 {
// Global variables Number of cameras
int res = 0;
public int minCameraCover(TreeNode root) {
if(judge(root) == 0)res++;
return res;
}
public int judge(TreeNode root){
// If it is an empty node, return Monitored
// So the leaf node is Not monitored
if(root == null)return 2;
// Judge the attributes of this node according to the left and right children
int left = judge(root.left);
int right = judge(root.right);
// There is one on the left and right Not monitored Camera
if(left == 0 || right == 0){
res++;
return 1;
}
// There is one on the left and right camera Is monitored
if(left == 1 || right == 1)return 2;
// The rest is left and right Monitored Is not monitored Give it to the node on the upper layer to add a camera
return 0;
}
}
Reference link :https://leetcode.cn/problems/binary-tree-cameras/solution/tan-xin-by-lafe-d-to4v/
边栏推荐
- NC50965 Largest Rectangle in a Histogram
- Commodity information management system (C language document version)
- PMP项目整合管理
- 杰理之内置关机电流 1.2uA,之后不能长按开机【篇】
- U++ learning note pile
- 【洛谷P1541】乌龟棋【DP】
- Jerry's modification does not require long press the boot function [chapter]
- Local dealers play the community group purchase mode and share millions of operations
- Oracle-PL/SQL编程
- 小鹏P7出事故,安全气囊未弹出,这正常吗?
猜你喜欢
【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
MySQL查询附近的数据.并按距离进行排序.
杰理之、产线装配环节【篇】
P7072 [CSP-J2020] 直播获奖
[LeetCode] 反转字符串中的单词 III【557】
位的高阶运算
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
大话云原生之负载均衡篇-小饭馆客流量变大了
Dahua cloud native load balancing article - the passenger flow of small restaurants has increased
kubernetes 使用主机名将 pod 分配在指定节点上
随机推荐
Go 4 modes Singleton
go 4种单例模式
[foreign journal] sleep and weight loss
uniapp微信登录返显用户名和头像
全面解析分享购商业模式逻辑?分享购是如何赋能企业
数组进阶提高
Baidu AI Cloud - create a face recognition application
牛客网:最大子矩阵
【洛谷P1541】乌龟棋【DP】
[leetcode] reverse string [344]
SimpleITK使用——3. 常见操作
原生js添加样式的方法
php实现根据输入的年龄查询出生日期符合的数据
PHP optimizes SQL queries in foreach
【板栗糖GIS】arcscene—如何做出有高度的高程图
解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
Jatpack------LiveData
杰理之、产线装配环节【篇】
[LeetCode] 反转字符串【344】
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)