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

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

Video file too large? Use ffmpeg to compress it losslessly

ucore lab5

C# Any()和AII()方法

反编译jar包,修改后重新编译为jar包

详细记录YOLACT实例分割ncnn实现

Apache POI的读写

Decompile the jar package and recompile it into a jar package after modification

Bluetooth health management device based on stm32

视频文件太大?使用FFmpeg来无损压缩它
随机推荐
使用aspose-slides将ppt转pdf
Some exercises about binary tree
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy
如何获取GC(垃圾回收器)的STW(暂停)时间?
Use of bufferedwriter and BufferedReader
Preliminary understanding of pytorch
unity--newtonsoft. JSON parsing
Vector:: data() pointer usage details
最全H桥电机驱动模块L298N原理及应用
6月23日《Rust唠嗑室》第三期B站视频地址
C# Any()和AII()方法
Use CAS to complete concurrent operations with atomic variables
Location and solution of network connection failure of primary online mobile terminal Report
R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行center中心化(每个数据列减去平均值)、设置method参数为center
Decompile the jar package and recompile it into a jar package after modification
openpyxl表格读取实例
视频文件太大?使用FFmpeg来无损压缩它
TDengine 邀请函:做用技术改变世界的超级英雄,成为 TD Hero
Arduino PROGMEM静态存储区的使用介绍
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