当前位置:网站首页>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/
边栏推荐
猜你喜欢

Leetcode circular linked list (fast and slow pointer) code line by line interpretation
![NC24325 [USACO 2012 Mar S]Flowerpot](/img/cf/86acbcb524b3af0999ce887c877781.png)
NC24325 [USACO 2012 Mar S]Flowerpot
![[leetcode] most elements [169]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[leetcode] most elements [169]

Uniapp wechat login returns user name and Avatar

Oracle cursor

SimpleITK使用——4. 奇怪的問題

图形视图框架

数组进阶提高
![Gas station [problem analysis - > problem conversion - > greed]](/img/15/5313f900abedb46ce82d8ab81af1d7.png)
Gas station [problem analysis - > problem conversion - > greed]

数学建模——图与网络模型及方法(一)
随机推荐
Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
UE4 UI adaptive screen
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
Qt QScrollArea
[leetcode] reverse the word III in the string [557]
What is the'function'keyword used in some bash scripts- What is the 'function' keyword used in some bash scripts?
[foreign journal] sleep and weight loss
Unity publishes a method of webgl playing sound
#include errors detected. Please update your includePath.
【外刊】睡眠与减肥
Gas station [problem analysis - > problem conversion - > greed]
Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
【板栗糖GIS】arcscene—如何做出有高度的高程图
Higher order operation of bits
Local dealers play the community group purchase mode and share millions of operations
MySQL查询附近的数据.并按距离进行排序.
[leetcode] most elements [169]
Xiaopeng P7 had an accident and the airbag did not pop up. Is this normal?
stop slave卡住--事务的事件没有复制完整
[羊城杯2020]easyphp