LeetCode 968. 监控二叉树
题目描述
题目描述
xxx
提示:
一、解题关键词
二、解题报告
1.思路分析
2.时间复杂度
3.代码示例
class Solution {
int result ;
public int minCameraCover(TreeNode root) {
//最值
//遍历树 树的打家劫舍
if(dfs(root) == 0){
result++;
}
return result;
}
int dfs(TreeNode root){
if(root == null){
return 1;
}
int left = dfs(root.left),right = dfs(root.right);
if(left == 0 || right == 0){
result ++;
return 2;
}
if(left == 1 &&right == 1){
return 0;
}
if(left + right >= 3){
return 1;
}
return - 1;
}
}
2.知识点
本文详细解析了LeetCode上的第968题——监控二叉树。通过深度优先搜索策略,实现树的遍历,并找到放置最少监控摄像头的位置,以覆盖所有节点。解题过程中,重点在于理解二叉树的特性和状态转移,以及如何优化时间复杂度。代码示例中展示了如何递归地进行节点的处理。
2290

被折叠的 条评论
为什么被折叠?



