Given a binary tree, we install cameras on the nodes of the tree.

Each camera at a node can monitor its parent, itself, and its immediate children.

Calculate the minimum number of cameras needed to monitor all nodes of the tree.

Example 1:

Input: [0,0,null,0,0]
Output: 1
Explanation: One camera is enough to monitor all nodes if placed as shown. Example 2:
Input: [0,0,null,0,null,0,null,null,0]
Output: 2
Explanation: At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.

Note:

    1. The number of nodes in the given tree will be in the range [1, 1000].
    2. Every node has value 0.

做了很久,还是写不出来,看了网上的答案。确实很精妙。利用了返回值表达了三种状态,利用引用存储最后结果。

还有一点就是在放相机的时候,在递归函数里,父节点一定比节点有优势,能放父节点一定放父节点,拿叶节点来

举例,此时如果放叶节点,能影响到的点只有它本身和父节点,而父节点能影响到子节点,本身,和它的父节点。

所以这是一个贪心算法。

class Solution {
public:
int minCameraCover(TreeNode* root) {
int sum=;
if(dfs(root,sum)==) sum++;// if root is not monitored, we place an additional camera here
return sum;
} int dfs(TreeNode * tr, int& sum){
if(!tr) return ;
int l=dfs(tr->left,sum), r=dfs(tr->right,sum);
if(l==||r==){// if at least 1 child is not monitored, we need to place a camera at current node
sum++;
return ;
}else if(l==||r==){// if at least 1 child has camera, the current node if monitored. Thus, we don't need to place a camera here
return ;
}else{// if both children are monitored but have no camera, we don't need to place a camera here. We place the camera at its parent node at the higher level.
return ;
}
return -;// this return statement won't be triggered
}
};

LC 968. Binary Tree Cameras的更多相关文章

  1. leetcode 968. Binary Tree Cameras

    968. Binary Tree Cameras 思路:如果子节点只能覆盖到父节点.当前节点,但是父节点可以覆盖到他的父节点.子节点.当前节点,所以从叶子节点往上考虑 0代表子节点没有被覆盖 1代表子 ...

  2. 【LeetCode】968. Binary Tree Cameras 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. [Swift]LeetCode968.监控二叉树 | Binary Tree Cameras

    Given a binary tree, we install cameras on the nodes of the tree. Each camera at a node can monitor  ...

  4. LC 94. Binary Tree Inorder Traversal

    问题描述 Given a binary tree, return the inorder traversal of its nodes' values. (左 - 根 - 右) Example: In ...

  5. [LC] 314. Binary Tree Vertical Order Traversal

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  6. [LC] 156. Binary Tree Upside Down

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  7. [LC] 298. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  8. [LC] 102. Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  9. [LC] 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

随机推荐

  1. 浅谈HTTP中Get与Post的区别【转】

    转自http://www.cnblogs.com/hyddd/archive/2009/03/31/1426026.html#commentform 感谢LZ分享 Http定义了与服务器交互的不同方法 ...

  2. MySQL存储过程01

    过程:封装了若干条语句,调用时,这些封装体执行 函数:是一个由返回值的’过程‘ 过程是没有返回值的函数 我们把若干条sql封装起来,起个名字---过程 把此过程存储在数据库中------存储过程 存储 ...

  3. k8s之pod与Pod控制器

    k8s中最为重要的基础资源,pod,pod controller,service pod controller类型有多种需要向控制器赋值之后使用: kubectl命令使用 kubectk get no ...

  4. A quick introduction to Google test

    视频参考:Google C++ Testing GTest GMock Framework 为什么要使用 Google C++ Testing Framework? 使用这个框架有许多好理由.本文讨论 ...

  5. 关于 ES5 & ES6 数组遍历的方法

    ES5 数组遍历方法 1.for 循环 , , , , ] ; i < arr.length; i++) { console.log(arr[i]) } 2.forEach , , , , ] ...

  6. 自动化测试常用断言的使用方法(python+selenium)

    自动化测试常用断言的使用方法(python) 自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断. 这 ...

  7. 鸡尾酒排序Cocktail Sort (双向冒泡排序)

    鸡尾酒排序 鸡尾酒排序思路,先从左边开始进行冒泡排序,第一趟冒泡排序完,最大值在的数组的最右端,然后进行第二趟排序,第二趟排序从右边开始排序,第二趟结束时,最小值在数组最左端,以此类推,每一趟排序完都 ...

  8. canvas绚丽的随机曲线

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyMAAAHECAIAAAClb2KBAAAgAElEQVR4nOyd+VsaV/v/Pz/UpW3abJ ...

  9. np.mean()函数

    1. 数组的操作: import numpy as np a = np.array([[1, 2], [3, 4]]) print(a) print(type(a)) print(np.mean(a) ...

  10. [51 Nod 1584] 加权约数和

    题意 求∑i=1N∑j=1Nmax(i,j)⋅σ1(ij)\large \sum_{i=1}^N\sum_{j=1}^Nmax(i,j)\cdot\sigma_1(ij)i=1∑N​j=1∑N​max ...