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.知识点