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

Addition, deletion, modification and query of handwritten ORM (object relationship mapping)

Source code analysis - lightweight asynchronous crawler framework Ruia
![NC24325 [USACO 2012 Mar S]Flowerpot](/img/cf/86acbcb524b3af0999ce887c877781.png)
NC24325 [USACO 2012 Mar S]Flowerpot

【外刊】睡眠与减肥

Xiaopeng P7 had an accident and the airbag did not pop up. Is this normal?

wait解决僵尸进程

位的高阶运算

Dynamic memory allocation (malloc calloc realloc free)
![[foreign journal] sleep and weight loss](/img/81/42dcfae19e72a0bc761cb7a40fe5d5.jpg)
[foreign journal] sleep and weight loss

【喜欢的诗词】好了歌
随机推荐
E-commerce system microservice architecture
Golang面试整理 三 简历如何书写
Server response status code
LeetCode 968. 监控二叉树
Using rendertext() to output multiple lines of text with rendertext() in R shiny
SimpleITK使用——4. 奇怪的問題
Oracle-PL/SQL编程
Rails 3 activerecord: sort by association count - rails 3 activerecord: order by count on Association
NC50965 Largest Rectangle in a Histogram
Introduction and response to high concurrency
对象与对象变量
[leetcode] there are duplicate elements [217]
What is the'function'keyword used in some bash scripts- What is the 'function' keyword used in some bash scripts?
Zhong Xuegao responded that the product will not melt for 1 hour: it contains solid components and cannot melt into water
数组进阶提高
杰理之修改不需要长按开机功能【篇】
解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
杰理之样机在多次触摸后会触发关机【篇】
Golang interview finishing three resumes how to write
`${}`的用法