当前位置:网站首页>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 !
边栏推荐
- Freemarker
- 文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument
- The R language uses the preprocess function of the caret package for data preprocessing: Center all data columns (subtract the average value from each data column), and set the method parameter to cen
- 为智能设备提供更强安全保护 科学家研发两种新方法
- 快速入门CherryPy(1)
- 借助原子变量,使用CAS完成并发操作
- 最全H桥电机驱动模块L298N原理及应用
- Apache POI的读写
- Product strength benchmarking seal /model 3, with 179800 pre-sales of Chang'an dark blue sl03
- Tdengine invitation: be a superhero who uses technology to change the world and become TD hero
猜你喜欢

【系统设计】邻近服务
![[system design] proximity service](/img/02/57f9ded0435a73f86dce6eb8c16382.png)
[system design] proximity service

On anchors in object detection

谷歌浏览器 chropath插件

Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
为智能设备提供更强安全保护 科学家研发两种新方法

leetcode:968. 监控二叉树【树状dp,维护每个节点子树的三个状态,非常难想权当学习,类比打家劫舍3】

使用aspose-slides将ppt转pdf

详解各种光学仪器成像原理

【OpenCV 例程200篇】211. 绘制垂直矩形
随机推荐
Freemarker
最全H桥电机驱动模块L298N原理及应用
unity--newtonsoft. JSON parsing
基于STM32设计的蓝牙健康管理设备
初步认识pytorch
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
为智能设备提供更强安全保护 科学家研发两种新方法
ucore lab5
R语言plotly可视化:plotly可视化基础小提琴图(basic violin plot in R with plotly)
When does the mobile phone video roll off?
CPU design (single cycle and pipeline)
Video file too large? Use ffmpeg to compress it losslessly
更改pip镜像源
技术与业务同等重要,偏向任何一方都是错误
Understand neural network structure and optimization methods
新旧两个界面对比
【报名】基础架构设计:从架构热点问题到行业变迁 | TF63
12 necessary tools for network engineers
SVN版本控制器的安装及使用方法
[system design] proximity service