当前位置:网站首页>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/
边栏推荐
- Dahua cloud native load balancing article - the passenger flow of small restaurants has increased
- 【喜欢的诗词】好了歌
- [leetcode] there are duplicate elements [217]
- 【洛谷P1541】乌龟棋【DP】
- NC24325 [USACO 2012 Mar S]Flowerpot
- [LeetCode] 反转字符串【344】
- Golang面试整理 三 简历如何书写
- Higher order operation of bits
- UE4 UI adaptive screen
- Solve the error of changing the selected file when uploading excel file. Net:: err_ UPLOAD_ FILE_ CHANGED
猜你喜欢
P7072 [CSP-J2020] 直播获奖
性能优化----严苛模式
Higher order operation of bits
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
Struct, bit segment, enumeration, union
【外刊】睡眠与减肥
位的高阶运算
`Usage of ${}`
【板栗糖GIS】arcscene—如何做出有高度的高程图
大话云原生之负载均衡篇-小饭馆客流量变大了
随机推荐
Server response status code
Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
Il n'est pas nécessaire d'appuyer longtemps sur la fonction de démarrage pour modifier Jelly [chapitre]
Golang interview finishing three resumes how to write
[羊城杯2020]easyphp
Storage unit conversion
原生js添加样式的方法
从2022年Q1财报看携程的韧性和远景
[leetcode] there are duplicate elements [217]
Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
[foreign journal] sleep and weight loss
Performance optimization - rigorous mode
Hanging mirror security won four global infosec awards on rsac2022
Uniapp wechat login returns user name and Avatar
Array advanced improvement
全面解析分享购商业模式逻辑?分享购是如何赋能企业
杰理之直接触摸样机的顶针反应不正常【篇】
数学建模——图与网络模型及方法(一)
佩服,竟然有人把高等数学这么晦涩难懂的科目,讲解得如此通俗易懂