968. Binary Tree Cameras

968. Binary Tree Cameras

Description

You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.

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

Example 1:

1
2
3
Input: root = [0,0,null,0,0]
Output: 1
Explanation: One camera is enough to monitor all nodes if placed as shown.

Example 2:

1
2
3
Input: root = [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.

Constraints:

  • The number of nodes in the tree is in the range [1, 1000].
  • Node.val == 0

Hints/Notes

  • binary tree

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
* right(right) {}
* };
*/
class Solution {
public:
int res = 0;

int minCameraCover(TreeNode* root) {
setCamera(root, false, 0);
return res;
}

// the meaning of the return value:
// 1. -1: the node doesn't exist, or it's lit by its children
// 2. 0: the node is leaf
// 3. 1: the node has one camera
int setCamera(TreeNode* root, bool hasParent, int depth) {
if (!root) {
return -1;
}

int l = setCamera(root->left, true, depth + 1);
int r = setCamera(root->right, true, depth + 1);

if (l == -1 && r == -1) {
if (hasParent) {
return 0;
} else {
res++;
return 1;
}
}

if (l == 0 || r == 0) {
res++;
return 1;
}

// the above conditions have ensured that there's
// at least one 1 in l and r
return -1;
// if (l == 1 || r == 1) {
// return -1;
// }
}
};