-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0968-binary-tree-cameras.cpp
More file actions
33 lines (27 loc) · 914 Bytes
/
0968-binary-tree-cameras.cpp
File metadata and controls
33 lines (27 loc) · 914 Bytes
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
class Solution {
public:
int minCameraCover(const TreeNode* const root) {
int count = 0;
const function<Type(const TreeNode*)> solve = [&](const TreeNode* const node) {
if (node == nullptr) {
return Type::NONE;
}
const auto l = solve(node->left);
const auto r = solve(node->right);
if (l == Type::NONE && r == Type::NONE) {
return Type::LEAF;
}
if (l == Type::LEAF || r == Type::LEAF) {
++count;
return Type::INODE;
}
// Pretend this node doesn't exist because it's monitored by
// a camera installed on a child node
return Type::NONE;
};
const auto type = solve(root);
return count + (type == Type::LEAF);
}
private:
enum class Type { NONE, LEAF, INODE };
};