当前位置:网站首页>leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]
leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]
2022-06-27 09:51:00 【Review of the white speed Dragon King】

analysis
Three states , Record the subtree with each node as the root :
state a:root When the camera must be placed , The number of cameras needed to cover the entire tree .
state b: The number of cameras needed to cover the entire tree , No matter what root Whether to place the camera .
state c: Number of cameras required to cover two subtrees , Regardless of node root Whether it is monitored .
a >= b >= c
a = lc + rc + 1
b = min(a, la + rb, ra + lb)
c = min(a, lb + rb)
Formula 1: The current root has been placed , Just cover two subtrees ( Lower requirements )
Formula 2: If you let it go root Namely a, If not, at least one of the left and right children will be released , Put it on the left and follow it on the right la + rb, Put right and follow left ra + lb
Formula 3: If you let it go root Namely a, If you don't put , That is, the possibility of two subtrees covering
If it's empty , Obviously, you can't choose , therefore a fill inf
ac code
class Solution:
def minCameraCover(self, root: TreeNode) -> int:
# Trees dp: Three states ( unimaginable )
# state a:root When the camera must be placed , The number of cameras needed to cover the entire tree .
# state b: The number of cameras needed to cover the entire tree , No matter what root Whether to place the camera .
# state c: Number of cameras required to cover two subtrees , Regardless of node root Whether it is monitored .
# a >= b >= c
def dfs(root: TreeNode) -> List[int]:
if not root:
return [float("inf"), 0, 0] # Impossible , use inf
la, lb, lc = dfs(root.left)
ra, rb, rc = dfs(root.right)
a = lc + rc + 1
b = min(a, la + rb, ra + lb)
c = min(a, lb + rb)
#print(a, b, c)
return [a, b, c]
a, b, c = dfs(root)
return b
summary
Trees dp Board question
How to think of the three states ? lament one 's littleness before the vast ocean !
边栏推荐
- torch.utils.data.RandomSampler和torch.utils.data.SequentialSampler的区别
- mongodb跨主机数据库拷贝以及常用命令
- 邮件系统(基于SMTP协议和POP3协议-C语言实现)
- 我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
- Quartz (timer)
- DNS standby server information, DNS server address (how many DNS preferred and standby are filled in)
- Use of bufferedwriter and BufferedReader
- Freemarker
- C# Any()和AII()方法
- prometheus告警流程及相关时间参数说明
猜你喜欢

On anchors in object detection
为智能设备提供更强安全保护 科学家研发两种新方法

Apache POI的读写

邮件系统(基于SMTP协议和POP3协议-C语言实现)

Markem imaje马肯依玛士喷码机维修9450E打码机维修

Advanced mathematics Chapter 7 differential equations

Source insight 工具安装及使用方法

微信小程序学习之五种页面跳转方法.
Shortcut key bug, reproducible (it seems that bug is the required function [funny.Gif])

产品力对标海豹/Model 3,长安深蓝SL03预售17.98万起
随机推荐
Bluetooth health management device based on stm32
Principle and application of the most complete H-bridge motor drive module L298N
详细记录YOLACT实例分割ncnn实现
ucore lab4
Conception de plusieurs classes
[STM32] Hal library stm32cubemx tutorial 12 - IIC (read AT24C02)
Use CAS to complete concurrent operations with atomic variables
详解各种光学仪器成像原理
有關二叉樹的一些練習題
R language plot visualization: visualize the normalized histograms of multiple data sets, add density curve KDE to the histograms, set different histograms to use different bin sizes, and add edge whi
微信小程序学习之五种页面跳转方法.
Es update values based on Index Names and index fields
视频文件太大?使用FFmpeg来无损压缩它
最全H桥电机驱动模块L298N原理及应用
2-4Kali下安装nessus
新旧两个界面对比
TDengine 邀请函:做用技术改变世界的超级英雄,成为 TD Hero
R语言plotly可视化:plotly可视化二维直方图等高线图、在等高线上添加数值标签、自定义标签字体色彩、设置鼠标悬浮显示效果(Styled 2D Histogram Contour)
This application failed to start because it could not find or load the QT platform plugin
Collection framework generic LinkedList TreeSet